Strings in UiPath
Strings are one of the most commonly used data types in UiPath. They represent sequences of characters, such as text, numbers, or symbols. Whether you're working with names, file paths, or dynamic messages, strings play a key role in automating processes.
In this guide, we will cover:
- What strings are
- Methods to work with strings
- Naming conventions for string variables
- String manipulation techniques
- Examples of practical usage
What is a String?
A string is a collection of characters stored as a single value. For example:
- Text: "Hello, World!"
- Numbers as Text: "12345"
- Special Characters: "$%^&*"
Strings are used in UiPath to store and manipulate textual information. They can be simple or complex, depending on your automation needs.
How to Create String Variables in UiPath
You can create string variables in UiPath using the following methods:
-
Using the Variables Panel:
- Open the Variables panel in your workflow.
- Click the
+
button to create a new variable. - Set the
Type
toString
and give it a meaningful name.
-
Inline Creation:
- In the Properties panel of an activity, type a new variable name.
- Use
Ctrl + K
to create it instantly and set its type toString
.
-
Default Assignment:
- Assign a default value to the string variable directly when creating it. For example,
"Welcome to UiPath!"
.
- Assign a default value to the string variable directly when creating it. For example,
Naming Conventions for String Variables
To make your workflows clear and organized, follow these naming conventions:
-
Use Descriptive Names:
- Avoid generic names like
str1
ortext
. - Use names like
customerName
orfilePath
to indicate the variable’s purpose.
- Avoid generic names like
-
Follow Camel Case:
- Start with a lowercase letter and capitalize the first letter of subsequent words.
- Example:
strInvoiceNumber
.
-
Add Prefixes:
- Use prefixes like
str_
for clarity. - Example:
str_userMessage
.
- Use prefixes like
-
Avoid Special Characters:
- Use only letters, numbers, and underscores.
Common String Methods in UiPath
UiPath provides several methods to manipulate strings effectively. Here are some of the most commonly used ones:
-
Length:
- Gets the number of characters in a string.
- Example:
strInput.Length
returns11
for "Hello World".
-
Substring:
- Extracts a part of a string.
- Example:
strInput.Substring(0, 5)
returns "Hello".
-
Split:
- Splits a string into an array based on a delimiter.
- Example:
strInput.Split(" ".ToCharArray)
splits "Hello World" into{"Hello", "World"}
.
-
Replace:
- Replaces occurrences of a specified value.
- Example:
strInput.Replace("World", "UiPath")
returns "Hello UiPath".
-
Contains:
- Checks if a string contains a specific value.
- Example:
strInput.Contains("Hello")
returnsTrue
for "Hello World".
-
ToUpper/ToLower:
- Converts a string to uppercase or lowercase.
- Example:
strInput.ToUpper()
returns "HELLO WORLD".
-
Trim:
- Removes leading and trailing whitespaces.
- Example:
strInput.Trim()
for " Hello World " returns "Hello World".
-
IndexOf:
- Finds the position of a character or substring.
- Example:
strInput.IndexOf("World")
returns6
for "Hello World".
-
Join:
- Combines an array of strings into a single string.
- Example:
String.Join(",", arrNames)
returns "Alice,Bob,Charlie".
-
Format:
- Formats strings with placeholders.
- Example:
String.Format("Hello {0}", "Alice")
returns "Hello Alice".
String Manipulation Techniques
-
Concatenation:
- Combines two or more strings into one.
- Example:
Output: "Hello UiPath"strGreeting = "Hello" + " " + "UiPath"
-
Dynamic Strings:
- Use variables within strings.
- Example:
Output: "Welcome, Alice!"strMessage = $"Welcome, {userName}!"
-
Handling Null or Empty Strings:
- Check if a string is null or empty.
- Example:
String.IsNullOrEmpty(strInput)
-
Extracting Specific Data:
- Use
Substring
,Split
, orRegex
to extract specific parts of a string. - Example: Extracting a file extension from a file path:
strExtension = filePath.Split("."c).Last()
- Use
-
Padding Strings:
- Add characters to the start or end of a string.
- Example:
Output: "0000001234"strPadded = strInput.PadLeft(10, "0"c)
Practical Examples
-
Creating Dynamic File Names:
- Variable:
strFileName
- Value:
$"Report_{Now.ToString("yyyyMMdd")}.xlsx"
- Output: "Report_20250103.xlsx"
- Variable:
-
Extracting Email Domains:
- Variable:
strEmail
- Value: "user@example.com"
- Code:
Output: "example.com"strDomain = strEmail.Split("@"c)(1)
- Variable:
-
Generating Welcome Messages:
- Variable:
strUserName
- Value: "Alice"
- Code:
Output: "Welcome, Alice!"strMessage = $"Welcome, {strUserName}!"
- Variable:
-
Validating Input Data:
- Check if an input string is in the expected format.
- Example:
If strInput.Contains("@") Then strResult = "Valid Email" Else strResult = "Invalid Email" End If
Best Practices for Strings
-
Use Descriptive Names:
- Make variable names meaningful, like
strUserEmail
instead oftext1
.
- Make variable names meaningful, like
-
Initialize Strings:
- Always assign a default value to avoid null reference errors.
- Example:
strInput = ""
-
Optimize String Operations:
- Avoid excessive concatenation in loops; use
StringBuilder
for better performance in such cases.
- Avoid excessive concatenation in loops; use
-
Handle Edge Cases:
- Ensure proper error handling for null, empty, or invalid strings.
-
Document Complex Logic:
- Add comments to explain complex string manipulations for better understanding.
Conclusion
Strings are incredibly versatile and essential for automating tasks in UiPath. By mastering string methods and manipulation techniques, you can handle text-based operations efficiently in your workflows. Start exploring these methods and apply them to your automation projects to streamline data processing and achieve dynamic results!
Follow us