How to Split a String

Hi!

I have a similar problem also, and I cannot get it to work with these tips. I would have to split a string by empty lines. Heres an example of a string:

Name 2
Name 345
Name 1

Name 1
Name 2
Name 3
1234

So, I would have to split it from where ever there’s an empty line. So it would become two strings:

Name 2
Name 345
Name 1

and

Name 1
Name 2
Name 3
1234.

I tried intructions from here: c# - How do I split a string on an empty line using .Split()? - Stack Overflow
but I could not get it to work with UiPath. I tried
text.Split({“\r\n\r\n”},stringsplitoptions.None)
but it doesn’t work.

Anyone have an idea what could I do? Expertise would be appreciated! Thank you very much!

2 Likes

Actually I got this to work!

text.Split({Environment.NewLine + Environment.NewLine},stringsplitoptions.none)

So, Environment.NewLine works atleast on a .txt file.

6 Likes

you can use

text.Split(new string[ ] {Environment.NewLine + Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)

The combined Environment.NewLine + Environment.NewLine as this will only split when a new line is also followed by a new line and split accordingly.

String[ ] people = text.Split(new string[ ] { “\r\n\r\n” },StringSplitOptions.RemoveEmptyEntries);

This will also work.

Regards…!!
Aksh

6 Likes

Okay, thank you much!

3 Likes

there is a split activity… we can specify the property based on which we should split like space, tab or new line… if we use that activity it will be easy no need of coding required…

3 Likes

I know this is a old post, however I was stuck in split string and was working on resolution which I wanted to share. I wanted to get the file name using the “Path.GetFileName” function into a single variable. the sample workflow works fine, you just need to specify the complete path along with the file name under “Path.GetFileName” function. Sample uploaded.Get_FileName_String_Split.zip (2.2 KB)

3 Likes

I don’t see a “Split” activity in my list.

For the love of god… splitting a string shouldn’t be so nuanced that it requires this much discussion :-/

Given SomeString contains a multi-word string, I tried simply using the Assign activity to assign SomeString.Split(" ") to a string array variable… and I keep getting the an error “Option Strict On disallows implicit conversions from string to char”.

I ended up having to Google the error message and change my approach to SomeString.Split(Convert.ToChar(" "))

5 Likes

Yeah, the Split function requires a character or character array.
Alternatively you can use ‘c’ or (0) to indicate that it’s a character.
SomeString.Split(" "c) or SomeString.Split(" "(0))

You can also use a string wtih brackets to make it an array.
SomeString.Split({" "},System.StringSplitOptions.None)
and the string can be anything. System.StringSplitOptions.RemoveEmptyEntries will also get rid of empty items.

Hope that gives you some additional info!
Thanks.

15 Likes

Thanks!

I don’t use VB much so I totally missed the () after String in this signature. That definitely clears it up.

split

2 Likes

hi

actual str=“123-456-6789”

i need to split like this dynamically

str1=“123”
str2=“456”
str3=“6789”

pls send me how to split and save it to another variable

1 Like

Hello @Venkatp,

You can use Microsoft.Activities.Expressions.SplitString Activity or String.Split Method. In both cases the result is an array of strings

SplitStringActivity.xaml (5.9 KB)
SplitStringAssign.xaml (6.1 KB)

Regards,
Susana

8 Likes

thank you all

Suppose ABC variable is stored with value “This is not game”
String ABC=“This is not a game”

There is a Split function available in String Class.
It operates two parameter
1.String name on which the operation should be performed
2.Delimiter for the string

It returns String of array, so use appropriate code.

String array_of_string=Split(ABC,“not”)
OR
String array_of_string=String.Split(ABC,“not”) --------// To be specific

2 Likes

Split(str.ToString,“-”)

3 Likes

Hi,

I am trying to split a string 14;687$ into 14 and 687 for further processing. Could someone tell me how this should be done? Thanks!

Hi @merlinazzz ,

string->stramount->14;687$

stramount.split(“;“c)(0)—>14
stramount.split(”;“c)(1).replace(”$”,“”)---->687

Regards,
Arivu

7 Likes

great, thanks a lot!

please I want to split from variable like this the name of the file is Rabeaalhaj _1234_basic.pdf here I want to put the name in one variable and the ID in another variable and the word basic in variable also so please how can I split them

Hi @rabea_alhaj,

Refer

Regards,
Arivu

4 Likes

“Please take note of your order reference: 156”
I want to separate 156 from the above expression and want to write in an excel. I just separated using a split string but when written in an excel it only writes “System.String” instead of 156.