Split Text using line number

Hello everybody and thanks in advance for your help.

I would like to split a text but using the number of line of this text.
I’ll try to explain it as most precisely as possible.

Imagine you’ve a text which contain 30 lines. In a text area, i’m limited to 25 lines so i need to split my text into two parts: the first one containing max 25 lines and the second one containing the rest of the text.

Due to the fact that i can’t know in advance which character will be the last of the 25th line, how can i use the line number to split my text into two parts ?

Thank you :slight_smile:

Hi @OLMEDO_Christian

Welcome to the community

So each line can be of any length or are there specific number of characters that are considered as a line?

If each line has specific characters then use substring with the number of characters

Str.Substring(0,“Number of characters for 25 lines”)

else use str.SPlit(Environment.Newline,0,25)

This will split the string with 25th occurrence of new line character

cheers

cheers

Thanks @Anil_G

Yes each line can be of any lenght and your solution Str.Split(Environment.NewLine, 0, 25) is not working.

I tried Str.Split(Environment.Newline.ToCharArray, 25) and the result is an Array containing each new line.

Hi @OLMEDO_Christian

My bad use split and then use string.Join to join only the first 25 lines

cheers

we assume that the text has linebreaks vblf, vbCr, vbCrLf oor others
Ones this is analyzed we split the text and can use the skip and take method

Hi @OLMEDO_Christian can you try like this once

Var1 = yourstring.Substring(0,25)

Var2 = yourstring.replace(Var1," ").tostring

Hi @Learner007,

Thanks for your help. This will not work because of the fact that Substring will cut my string from the first character to the 25th character included but not from the first line to the 25th line.

when your case is still open and splitting the data is unclear due to the linebreak is not recognized, feel free to share with us some sample data as a text file. Let us also know from where the text content is retrieved (File, Webapp…)

Thanks

I’m finally using this method :

First => ArrayOfString = String1.Split(Environment.NewLine.ToCharArray()) where String1 is my full text

Second => Initialization of an Index to 0 and
While Index < 25
String3 = String.Concat(String2, ArrayOfString(Index)
String2 = String3
Index = Index + 1
where String2 will be my final result and String3, an intermediate result.

At the end, i’ve my Result, String2, which contains my 25 first line of the initial text from which i wanted the split.

Thx all for your help :slight_smile:

1 Like
ArrayOfString = String1.Split(Environment.NewLine.ToCharArray())
StrPartOne = String.Join(Environment.NewLine,ArrayOfString.Take(25)) 
StrPartTwo = String.Join(Environment.NewLine,ArrayOfString.Skip(25))

This solution works very well, thanks a lot @ppr :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.