Strings in UiPath

 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:

  1. Using the Variables Panel:

    • Open the Variables panel in your workflow.
    • Click the + button to create a new variable.
    • Set the Type to String and give it a meaningful name.
  2. 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 to String.
  3. Default Assignment:

    • Assign a default value to the string variable directly when creating it. For example, "Welcome to UiPath!".

Naming Conventions for String Variables

To make your workflows clear and organized, follow these naming conventions:

  1. Use Descriptive Names:

    • Avoid generic names like str1 or text.
    • Use names like customerName or filePath to indicate the variable’s purpose.
  2. Follow Camel Case:

    • Start with a lowercase letter and capitalize the first letter of subsequent words.
    • Example: strInvoiceNumber.
  3. Add Prefixes:

    • Use prefixes like str_ for clarity.
    • Example: str_userMessage.
  4. 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:

  1. Length:

    • Gets the number of characters in a string.
    • Example: strInput.Length returns 11 for "Hello World".
  2. Substring:

    • Extracts a part of a string.
    • Example: strInput.Substring(0, 5) returns "Hello".
  3. Split:

    • Splits a string into an array based on a delimiter.
    • Example: strInput.Split(" ".ToCharArray) splits "Hello World" into {"Hello", "World"}.
  4. Replace:

    • Replaces occurrences of a specified value.
    • Example: strInput.Replace("World", "UiPath") returns "Hello UiPath".
  5. Contains:

    • Checks if a string contains a specific value.
    • Example: strInput.Contains("Hello") returns True for "Hello World".
  6. ToUpper/ToLower:

    • Converts a string to uppercase or lowercase.
    • Example: strInput.ToUpper() returns "HELLO WORLD".
  7. Trim:

    • Removes leading and trailing whitespaces.
    • Example: strInput.Trim() for " Hello World " returns "Hello World".
  8. IndexOf:

    • Finds the position of a character or substring.
    • Example: strInput.IndexOf("World") returns 6 for "Hello World".
  9. Join:

    • Combines an array of strings into a single string.
    • Example: String.Join(",", arrNames) returns "Alice,Bob,Charlie".
  10. Format:

    • Formats strings with placeholders.
    • Example: String.Format("Hello {0}", "Alice") returns "Hello Alice".

String Manipulation Techniques

  1. Concatenation:

    • Combines two or more strings into one.
    • Example:
      strGreeting = "Hello" + " " + "UiPath"
      
      Output: "Hello UiPath"
  2. Dynamic Strings:

    • Use variables within strings.
    • Example:
      strMessage = $"Welcome, {userName}!"
      
      Output: "Welcome, Alice!"
  3. Handling Null or Empty Strings:

    • Check if a string is null or empty.
    • Example:
      String.IsNullOrEmpty(strInput)
      
  4. Extracting Specific Data:

    • Use Substring, Split, or Regex to extract specific parts of a string.
    • Example: Extracting a file extension from a file path:
      strExtension = filePath.Split("."c).Last()
      
  5. Padding Strings:

    • Add characters to the start or end of a string.
    • Example:
      strPadded = strInput.PadLeft(10, "0"c)
      
      Output: "0000001234"

Practical Examples

  1. Creating Dynamic File Names:

    • Variable: strFileName
    • Value: $"Report_{Now.ToString("yyyyMMdd")}.xlsx"
    • Output: "Report_20250103.xlsx"
  2. Extracting Email Domains:

    • Variable: strEmail
    • Value: "user@example.com"
    • Code:
      strDomain = strEmail.Split("@"c)(1)
      
      Output: "example.com"
  3. Generating Welcome Messages:

    • Variable: strUserName
    • Value: "Alice"
    • Code:
      strMessage = $"Welcome, {strUserName}!"
      
      Output: "Welcome, Alice!"
  4. 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

  1. Use Descriptive Names:

    • Make variable names meaningful, like strUserEmail instead of text1.
  2. Initialize Strings:

    • Always assign a default value to avoid null reference errors.
    • Example: strInput = ""
  3. Optimize String Operations:

    • Avoid excessive concatenation in loops; use StringBuilder for better performance in such cases.
  4. Handle Edge Cases:

    • Ensure proper error handling for null, empty, or invalid strings.
  5. 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!