Hi Everyone,
We often work with Strings and manipulation of strings when working on projects. I would like to cover the most frequently used functions here and hope it would be helpful for you.
With the help of these functions, we can avoid activities for simple functions.
Below two variables were used to cover the different functions.
str_CompanyA = " This is Company : A "
str_CompanyB = “This is Company: B”
-
Trim: Removes leading and trailing spaces from the string -
str_CompanyA.trim
-
TrimStart: Removes the spaces at the start of the string -
str_CompanyA.TrimStart
-
TrimEnd: Removes spaces at the end of the string -
str_CompanyA.TrimEnd
-
ToUpper: Converts all characters in string to upper case -
str_CompanyA.ToUpper
-
ToLower: Converts all characters in string to lower case -
str_CompanyA.ToLower
-
Length: Returns total number of characters present in the string -
str_CompanyA.length
-
len: Returns total number of characters present in the string -
len(str_CompanyA)
-
Left(n): Returns First n characters from string -
Left(str_CompanyA,5)
-
Right(n): Returns last n characters from string -
Right(str_CompanyA,5)
-
StrReverse: Reverse the given string -
StrReverse(str_CompanyA)
-
IndexOf(‘a’): Returns the position of first occurance of the specified text
str_CompanyA.IndexOf('a')
-
LastIndexOf(‘a’): Returns the position of last occurance of specified text
str_CompanyA.LastIndexOf('a')
- Split a string . Two different ways
split(str_CompanyA,",")
&str_CompanyA.Split(","c)
- Split based on new line. Try with vbCrLf & vbCr based on scenario.
str_CompanyA.Split(vbLf,StringSplitOptions.RemoveEmptyEntries)
-
Equals: Compare two strings. Output is boolean
str_CompantA.Equals(str_CompanyB)
-
StrComp: Compares two strings. Returns 0 if both strings are equal. Else -1
StrComp(str_CompanyA,str_CompanyB)
-
Replace(“a”,“b”): Replace all occurances of a with b in a given string
str_CompanyA.Replace("i","I")
-
Remove(1,5): returns the string with 5 characters removed starting from index 1
str_CompanyA.Remove(1,5)
-
Insert(index,string): Inserts a specified string or character in the specified index
str_CompanyA.Insert(1,"Hello")
-
IsNullOrEmpty: Returns true if string is null or empty else returns false
string.IsNullOrEmpty(str_CompanyA)
-
IsNullOrWhiteSpace: Returns true if string is null or whitespace else returns false
String.IsNullOrWhiteSpace(str_CompanyA)
-
Concat: Joins two or more strings
string.Concat(str_CompanyA," - ",str_CompanyB)
- Another way to Join strings
str_CompanyA+" - "+str_CompanyB
- Another way to Join strings
str_CompanyA&" - "&str_CompanyB
- Convert secure string to string
new System.Net.NetworkCredential(string.Empty,"Your Secure String").Password
- Convert String to secure string
(new System.Net.NetworkCredential(string.Empty, str_CompanyA)).SecurePassword
- Convert string to “Proper” or “Title” case
StrConv(str_CompanyA,VbStrConv.ProperCase)
-
contains(“a”): Returns true if specified text present in the string else false
str_CompanyA.contains("Hello")
- String.Format: Build a string based on a template -
String.Format("The active user is {0} and their favourite food is {1}.", strName, strFavFood)
- Strings.Join: Joins an array of strings with the given delimiter (to reverse a split string or summarize a list) -
Strings.Join({ "array", "of", "strings" }, ", ")
- If you ever need a " character in your strings, you need to double them up -
String.Format("The user typed ""{0}"".", strInput)
str_CompanyA.Split({ vbCrLf, vbLf }, StringSplitOptions.RemoveEmptyEntries)