How to Split a String

I am trying to split a string on string ‘not’ . String is “This is not a game” is in variable PDFVariable. So I use a variable Var of type String and assign as PDFVariable.ToString().Split(‘not’) and trying to see the output in Var(1). But I am getting error in the Assign box.

12 Likes

Can you send what error message are you getting when u hover on the blue warning.

May be you cannot use Var as a variable,try something else.

3 Likes

3 Likes

Did you try double quotes for not instead of single?

→ “not”

3 Likes

Does not work on “not”

4 Likes

Hi Sampad ,

use can use
Split(PDFvariable.tostring,“not”)

or you can use “Split string” activity in Expressions

36 Likes

Thanks Uday !! Works fine !

3 Likes

Hi @sampaddas,

You can try this StringVaraible.Split({“not”},StringSplitOptions.None)

Thanks,
Vikas Reddy :slight_smile:

22 Likes

Variable.Split({“not”},StringSplitOptions.None)…Try with this This will work…

8 Likes

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(" "))

7 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.

16 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