Split string line by line

Hi,
I want to split string line by line for that I used
strText.Split(Environment.NewLine.ToCharArray) giving exception “can not covert from ‘method group’ to ‘char’”

used strText.ToString.Trim.Split(Environment.NewLine.ToArray, StringSplitOptions.RemoveEmptyEntries) giving exception saying string.tostirng() is a method which is not valid in given context
text.Split(Environment.NewLine.ToArray,StringSplitOptions.RemoveEmptyEntries) does not work

Could it be the case that your project is set to C#?

You’re overcomplicating it.

image

Hi @akhtar.nadeem,

Try below one

strText.ToString().Split(Environment.NewLine **Or** Convert.ToString(strText).Split(Environment.NewLine)`

Regards,
Arivu

1 Like

Why are you converting a variable that’s already a string, into a string? This is unnecessary.

If suppose the string value is null will get an error so using .ToString

Regards,
Arivu

Using .ToString doesn’t avoid an error if the value is null. You’d have to check if it’s null before trying to split it.