How to manipulate a part of string: Split, Trim, Substring, Replace, Remove, Left, Right

Hey,
I had prepared a workflow file with examples and Excel file with an explanation of how you can manipulate a part of string in variables. Examples are for RPA Dev Rookies.

Examples_Split_Trim_Substring_Remove_Left_Right.xaml (30.8 KB)
ENG Split Trim Substring Left Right Replace Remove.xlsx (26.3 KB)
POL Operacje na zmiennych Split Trim Substring Left Right Replace Remove.xlsx (26.2 KB)

Split, Double Split, Trim, Substring, Replace, Remove, Left, Left+Substring, Right, Right+Substring

SPLIT
Spit divides the text into parts:
For: String on the part = (0): left, (1): right
For: String [.] on value of strings
Example 1
Split takes a part of text to ‘Array of strings’ - after “:” and saves it in memory of Variable type String[.]
Variable name = text
Variable type = Generic Value
New Variable name = new_text
Variable type = Array of Strings (String[.])
text = Hello:World:Its:Me
Assign:
new_text = Split(text,":")
Write Line:
new_text(3)
Variable ‘new_text’ contains now array of 4 strings
(0) = Hello
(1) = World
(2) = Its
(3) = Me
Result (Write Line) : Me

Example 2
Split takes a part to ‘Array of character’ - after " " (c = .ToCharArray)
Variable name = text
Variable type = Generic Value
New Variable name = new_text
Variable type = Array of Strings (String[.])
text = Hello World Its Me
Assign:
new_text = text.Split(" ".ToCharArray)
Write Line:
new_text(2)
Variable ‘new_text’ contains now array of 4 characters
(0) = Hello
(1) = World
(2) = Its
(3) = Me
Result (Write Line) : Its

Example 3
Holding the result after the Split operation in a variable with the type ‘String’
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = Hello World Its Me
Assign:
new_text = text.Split(" "c)(2).ToString
Write Line:
new_text
Result (Write Line) : Its

Example 4 Double SPLIT
The first Split divides the text after the phrase “: W” and leaves the part to the right (1)
Second Split divides the left text after the first “:” and leaves what is left (0)
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = Hello:World:Its:Me
Assign:
new_text = Split((Split(text,":W")(1).ToString),":")(0).ToString
Write Line:
new_text
Result (Write Line) : orld

Example 5 Double SPLIT
The first Split divides the text into the phrase “: M” and leaves a part to the left (0)
Second Split divides the left text after the first “o:” and leaves what is on the right (1)
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = Hello:World:Its:Me
Assign:
new_text = Split((Split(text,":M")(0).ToString),“o:”)(1).ToString
Write Line:
new_text
Result (Write Line) : World:Its

SUBSTRING
Example 1
Takes content 5 characters from the first character from the left (0)
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = Hello World Its Me
Assign:
new_text = text.Substring(0, 5)
Write Line:
new_text
Result (Write Line) : Hello

Example 2
Takes content 6 characters from the fourth character from the left (4)
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = Hello World Its Me
Assign:
new_text = text.Substring(4, 6)
Write Line:
new_text
Result (Write Line) : o Worl

Example 3
Takes contents of 3 characters from the end
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = Hello World Its Me
Assign:
new_text = text.Substring(text.Length -3)
Write Line:
new_text
Result (Write Line) : " Me"

TRIM
Trim deletes the leading and trailing spaces
Example 1
The value of ‘text’ contains “” to show that there are spaces in the text
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = " Hello World Its Me "
Assign:
new_text = text.Trim
Write Line:
new_text
Result (Write Line) : “Hello World Its Me”

REPLACE
Replace changes the specified character type to another
Example 1
The value of ‘text’ contains “” to show that there are spaces in the text
*Change " " (spaces) to " " (pipe)*
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = " Hello World Its Me "
Assign:
new_text = text.Replace(" ", “I”)
Write Line:
new_text
Result (Write Line) :

REMOVE
Remove removes the text that we describe in the logic (inverse to SUBSTRING)
Example 1
Deleting content 5 characters from the first character from the left (0)
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = Hello World Its Me
Assign:
new_text = text.Remove(0,5)
Write Line:
new_text
Result (Write Line) : World Its Me

LEFT
Retrieves the text from the variable from LEFT
Example 1
Gets the content 7 characters from the left
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = Hello World Its Me
Assign:
new_text = Left(text, 7)
Write Line:
new_text
Result (Write Line) : Hello W

LEFT + SUBSTRING
Gets the text from the variable counting from the LEFT of what is left after trimming by SUBSTRING
Example 1
Retrieves the content of 3 characters from the left counting from ‘cropped text to 5 characters’ counted from the 7th character of the phrase ‘text’
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = HelloWorldItsMe
Assign:
new_text = Left(text.Substring(7,5), 3)
Write Line:
new_text
Result (Write Line) : rld

RIGHT
Retrieves the text from the variable from RIGHT
Example 1
Gets the content 7 characters from the right
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = HelloWorldItsMe
Assign:
new_text = Right(text, 7)
Write Line:
new_text
Result (Write Line) : ldItsMe

RIGHT + SUBSTRING
Retrieves the text from the variable counting from the RIGHT from what is left after trimming by SUBSTRING
Example 1
Gets the content ‘3 characters from the right’ from ‘cropped text to 6 characters’ ‘from the beginning (0)’
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = HelloWorldItsMe
Assign:
new_text = Right(text.Substring(0,6), 3)
Write Line:
new_text
Result (Write Line) : loW

Examples_Split_Trim_Substring_Remove_Left_Right.xaml (30.8 KB) ENG Split Trim Substring Left Right Replace Remove.xlsx (26.3 KB) POL Operacje na zmiennych Split Trim Substring Left Right Replace Remove.xlsx (26.2 KB)

23 Likes

Hi
@Adrian_Star

Welcome to uipath community
That’s cool
great job…keep going
Cheers

1 Like

could you help me get the date from this string?

First Name Last Name\r\n2 September 2019\r\nPowered by TCPDF (www.tcpdf.org)

without using like the set numbers but rather the word “Powered” as the length of the entire string changes

Hello, You can use:

A) Split by char
Where:
Variable:
text = eg. Generic Value
new_text = String (Array of strings)

Input:
text = JOHN SMITH\r\n2 September 2019\r\nPowered by TCPDF (www.tcpdf.org)

Logic:
new_text = Split(text," "c)

  1. Exampe
    Write Line = new_text(0) + " " + Split(new_text(1),"\r\n2")(0)

Output: JOHN SMITH

  1. Example
    Write Line = new_text(0) + " " + Split(new_text(1),"\r\n2")(0) + " " + new_text(2) + " " + Split(new_text(3),"\r\n")(0)

Output: JOHN SMITH September 2019


B) Split by Split in String

  1. Example
    To Get FirstName and Last Name You can use also:

Variable:
text = eg. Generic Value
new_text = String

Logic:
new_text = Split(text,"\r\n2")(0).ToString

Write Line: new_text

Output: JOHN SMITH

  1. Example
    Additionally to Get “Month Date”

Logic:
new_text = Split((Split(text,"\r\n2 “)(1).ToString),”\r\n")(0).ToString

Write Line: new_text

Output: September 2019

3 Likes

@Adrian_Star Great post! Was very helpful to solve my string splitting.

Hi! I have a problem with string manipulation. How do you get the string value after the text ‘Dependent Verification -’

Example: Dependent Verification -Approved

I need to get the any string that comes after the ‘-’

Thanks!

Use split method like this
Split(str_input,”-“)(1).ToString

Cheers @caduque

2 Likes

Hi @Palaniyappan

Actually the value ‘Dependent Verification -’ is a row value.

So my code would be Split(row(5).ToString,”-“)(1).ToString ?

1 Like

Yah Correct
Cheers @caduque

1 Like

Hi @Adrian_Star

I moved your post to the #news:faq section, it’s great!

I suppose it could only use some better formatting, currently I think you simply copy pasted the text from other source and it looks a bit awkward :smiley:

2 Likes

Hey, I’m putting graphics for a better understanding of the subject.

5 Likes

Does anyone know how to remove the symbol " in a text? I tried

str_paymentpartner.Replace(""","")

but doesn’t seems to work :sweat_smile: :weary:

1 Like

@Nasya.Idris Please check below one.

str_paymentpartner.Replace("""","")

3 Likes

You can use logic:

System.Text.RegularExpressions.Regex.Replace(yourString, "[^\w\.@-]","")
or
System.Text.RegularExpressions.Regex.Replace(yourString, "[^\w\.@-]"," ")

regex

Or read this post:
Replace (regex) problems

3 Likes

Thank you!

2 Likes

bravo and I hope this gets added to some of the official documentation/ reference in academy. bump for visibility

3 Likes

Amazing! This is as if good for documentation in UiPath. I wish the UiPath Academy would also add some in-depth tutorials like this one for addressing specific functionalities in their courses. Great job @Adrian_Star!

3 Likes