How to split string by new line

How do I split a string variable by new lines?

I used the Get Text activity and stored the text into a variable called Address.

The output of the variable Address is:

123 Address St
New York, NY
123 456

How can I split the variable Address by new line?

When I use the Write Line activity, I get “123 Address St \r\n New York, NY \r\n 123 456”

I tried splitting the string using the Split method, but I get the error message stating its unable to convert from string to 1-dimensional array.

Thanks

@happygal321
One Option from Many possible options
yourString.Split(Environment.NewLine.TocharArray)

1 Like

Hi @happygal321 to add lines to a string you can use +Enviroment.NewLine+ between the text you want to paste.

@ppr @Artur_Bueno
This didn’t work for me. I get the error

“Compiler error(s) encountered processing expression`Address.Split(Environment.NewLine.TocharArray)”.Value of type ‘1-dimensional array of String’ cannot be converted to ‘String’.

I am trying to split the String variable Address into 3 new variables.
I.e.

Address = 123 Address St \r\n New York, NY \r\n 123 456

new variable Address1 = 123 Address St

new variable Address2 = New York, NY

new variable Address3= 123 456

@happygal321
Instead of assigning the result to a String Change the Datatype of receiving variable to String() does mean an Array of string

Accessing the different splits by e.g yourstringarrayvar(0) for getting the First line

Thanks, but when I do this, the Get Text activity where im storing the text as variable Address doesn’t work. I get the error “ArrayConverter cannot convert from System.String.”

@happygal321 -

try stringArray = Split(Address,vbcrlf)

can also try stringArray = Split(Address,"\r\n")

You will have values in stringArray[0], stringArray[1] and stringArray[2]

@happygal321


with a variation in statement:
testString.Split({Environment.NewLine},StringSplitOptions.RemoveEmptyEntries)

1 Like

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