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

Hi,
if you are a beginner in the world of automation and would like to know how you can manipulate text based on the solutions available in the VB.NET code and using REGEX (regular expressions) then you are in the right place.

In the topic below, you will learn how to easily extract data from any part of the search text using various methods.

The topic includes solution examples with descriptions and graphics to better understand the topic for functions such as:

  • Split
  • Substring
  • Left
  • Right
  • Replace
  • Remove
  • Trim, LTrim, RTrim
  • TrimStart
  • TrimEnd
  • Regex.Replace

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

Let’s get started:

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, LTRIM, RTRIM
Trim: function to strip (deletes) the leading and trailing spaces from a string variable.
LTrim: function to strip (deletes) leading spaces from a string variable.
RTrim: function to strip (deletes) trailing spaces from a string variable.
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
Or
new_text = Trim(text)
Or
new_text = RTrim(text), or LTrim(text)
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(" ", "
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

TrimSTART
TrimStart removes all leading occurrences
Example 1
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = “00000ABC123000”
Assign:
new_text = text.TrimStart("0"c)
Write Line:
new_text
Result (Write Line) : “ABC123000”

Example 2
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = “00000ABC”
Assign:
new_text = text.TrimStart({"0"c, "A"c})
Write Line:
new_text
Result (Write Line) : “BC”
TrimEND
TrimEnd removes all trailing occurrences
Example 1
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = “00000ABC123000”
Assign:
new_text = text.TrimEnd("0"c)
Write Line:
new_text
Result (Write Line) : “00000ABC123”

Example 2
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = “00000ABC-”
Assign:
new_text = text.TrimEnd({"-"c, "C"c})
Write Line:
new_text
Result (Write Line) : “00000AB”
REGEX.Replace
Example 1
Replace any non-word character to nothing (remove)
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = “000 abc 111-ABC@!&”
Assign:
new_text = System.Text.RegularExpressions.Regex.Replace(text, "\W","")
Write Line:
new_text
Result (Write Line) : “000abc111”

Example 2
Replace any word character (include digits) to nothing (remove)
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = “abc 123 !@#$%^&*()_”
Assign:
new_text = System.Text.RegularExpressions.Regex.Replace(text, "\w","")
Write Line:
new_text
Result (Write Line) : " !@#$%^&*()_"

Example 3
Replace any digit character to nothing (remove)
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = “A1B2C3D4E5F67890GH”
Assign:
new_text = System.Text.RegularExpressions.Regex.Replace(text, "\d","")
Write Line:
new_text
Result (Write Line) : “ABCDEFGH”

Example 4
Replace any non-digit character to nothing (remove)
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = “A1B2C3D4E5F67890GH”
Assign:
new_text = System.Text.RegularExpressions.Regex.Replace(text, "\D","")
Write Line:
new_text
Result (Write Line) : “123456789”

Example 5
Replace specific set of characters like: “A”, “^”, “!” to nothing (remove)
` ` pipe is separator in regex (alternative)
\ back slash is use to escape special character in regex (^ in regex set begining of expression)
Variable name = text
Variable type = String
New Variable name = new_text
Variable type = String
text = “A1B^C3D^E5F!!89^GH”
Assign:
new_text = `System.Text.RegularExpressions.Regex.Replace(text, "A
Write Line:
new_text
Result (Write Line) : “1BC3DE5F89GH”

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)

251 Likes

Hi
@Adrian_Star

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

6 Likes

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

16 Likes

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

2 Likes

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

8 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

2 Likes

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:

5 Likes

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

Moved to first entry.

13 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:

output = System.Text.RegularExpressions.Regex.Replace(your_String, "[^\w\.@-]","")

or

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

regex

Or read this post:
Replace (regex) problems

9 Likes

Thank you!

4 Likes

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

6 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!

7 Likes

Você esta de Parabéns!!!

2 Likes

This is amazing! Thank you so much for this.

3 Likes

Thank you very much man. You definitely simplified everything.

3 Likes