String manipulation is the process of analyzing a given string and applying different methods in order to replace, split, find characters or perform any other string transformations.
Knowing how to manipulate strings plays a crucial role in most text processing tasks. Some tasks can be as simple as replacing a character meanwhile, others can be very complex. So, knowing all possible string functions or methods can make your life much easier.
In this article, I will present the most common methods used by RPA developers:
Contains
StartsWith & EndsWith
Format
Replace
Split
Contains
I use the method to check whether a character or a sequence of characters exists in a given string. The method returns a Boolean value. The comparison is case-sensitive. The search begins at the first character position of this string and continues through the last character position. If the argument is not defined, the method throws an exception.
Examples
Check if the given text contains a letter or a word.
stringVar = “The quick brown fox jumps over the lazy dog”
Syntax | Result |
---|---|
stringVar.Contains(“fox”) | True |
stringVar.Contains(“Fox”) | False |
stringVar.Contains(“bear”) | False |
stringVar.Contains() | Error |
The second example returns False because the method is case-sensitive.
https://docs.microsoft.com/en-us/dotnet/api/system.string.contains?view=netframework-4.8
StartsWith & EndsWith
I use the method to check whether a given string starts or ends with a substring. The method returns a Boolean value. The call is case-sensitive. If the argument is not defined, the method throws an exception.
Examples
Check if the text begins or ends with a given letter or a word.
stringVar = “The quick brown fox jumps over the lazy dog”
Syntax | Result |
---|---|
stringVar.StartsWith(“T”) | True |
stringVar.StartsWith(“The”) | True |
stringVar.StartsWith(“THE”) | False |
stringVar.StartsWith(“The quick”) | True |
stringVar.StartsWith() | Error |
stringVar.EndsWith(“g”) | True |
stringVar.EndsWith(“G”) | False |
stringVar.EndsWith(“dog”) | True |
stringVar.EndsWith() | Error |
https://docs.microsoft.com/en-us/dotnet/api/system.string.startswith?view=netframework-4.8
https://docs.microsoft.com/en-us/dotnet/api/system.string.endswith?view=netframework-4.8
Format
From my point of view, this method is one of the most used String methods. I use it when I want to replace an array of strings within a string template. The method returns a String value. It throws an exception when the arguments are not defined or the format is wrong.
Examples
Syntax | Result |
---|---|
String.Format(“My name is {0} and I am {1} years old.”,”John”,”21″) | My name is John and I am 21 years old. |
String.Format(“Today is {0}.”,”Monday”) | Today is Monday. |
https://docs.microsoft.com/en-us/dotnet/api/system.string.format?view=netframework-4.8
Replace
I use the method when I want to replace a specific character or a string within a given string. The method returns a String value. It throws an exception when the first argument is not defined or is the empty string(“”).
Examples
Replace the comma with a semicolon, replace a letter with another letter, replace a letter with a string.
stringVar = “apple, orange, mango”
Syntax | Result |
---|---|
stringVar.Replace(“,”,”;”) | apple; orange; mango |
stringVar.Replace(“a”,”i”) | ipple, oringe, mingo |
stringVar.Replace(“a”,”ASD”) | ASDpple, orASDnge, mASDngo |
stringVar.Replace(“”,”;”) | Error |
https://docs.microsoft.com/en-us/dotnet/api/system.string.replace?view=netframework-4.8
Split
I use the method when I want to split a string into a number of substrings. The method returns an Array of Strings.
Examples
Split the given text by comma.
stringVar = “apple, orange, mango”
Syntax | Result |
---|---|
stringVar.Split(“,”) | {“apple”,”orange”,”mango”} |
https://docs.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.8