How Split String with multiple delimiters

Hi,

I’ve a scenario where in I’ve my string in multiple lines as below
str_Text=“test1 test2
test3 test4
test5 test6”
and I don’t want any empty values to be present in my split array. I’ve written the following expression:
str_Text.split({" "c,Environment.NewLine},StringSplitOptions.RemoveEmptyEntries).ToArray
Now My array looks like this
string[4]{“test1”,@“test2
test3”,@“test4
test5”,“test6”}

How can I get the last element in every line and the first element in newline split. I expect my array to have 6 elements which will look like
string[6]{“test1”,“test2”,“test3”,“test4”,“test5”,“test6”}

we can trim

str_Text.split({" "c},StringSplitOptions.RemoveEmptyEntries).Select(Function (x) x.Trim).ToArray

UPD1 - extended to linebreak as additional split char:

str_Text.split({chr(32),chr(13)}, StringSplitOptions.RemoveEmptyEntries).select(Function (x) x.Trim).toArray

@Babjee.Vangipurapu

  • Assign Activity:
  • Name: str_Text
  • Value: "test1 test2\ntest3 test4\ntest5 test6"
  • Assign Activity:
  • Name: lines
  • Value: str_Text.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
  • Assign Activity:
  • Name: splitWords
  • Value: lines.SelectMany(Function(line) line.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)).ToArray()
  • Write Line Activity:
  • Text: String.Join(",", splitWords)

Hi @ppr ,

Thanks for the reply. I don’t see any change in output, I needed the output as
string[6]{“test1”,“test2”,“test3”,“test4”,“test5”,“test6”}
My motive is to split the value at the newline as well as a space in the string

Your text:
grafik

Split and trim:
grafik

we added the linebreak as well

UPD1 - added statement code:

 str_Text.split({chr(32),chr(13)}, StringSplitOptions.RemoveEmptyEntries).select(Function (x) x.Trim).toArray

Hi @Krishna_Raj

Can you please share the xml file I’m getting errors when I try replicating the code

Hi @Babjee.Vangipurapu

Try this

Input.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).SelectMany(Function(line) line.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)).ToArray()

The image shows the Immediate window in a Visual Studio debugging session displaying a command that splits an input string by newline and space, removing empty entries, and the result is an array of six test strings. (Captioned by AI)

Regards,

Hi @lrtetala ,

Thank you so much this is working as expected.

1 Like

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