100 String manipulation examples

Category Example Sample Input Expected Output
Basic st.ToUpper hello HELLO
Basic st.ToLower HELLO hello
Basic st.Trim text text
Basic st.TrimStart text text
Basic st.TrimEnd text text
Basic st.Length text 4
Basic String.IsNullOrEmpty(st) True
Basic String.IsNullOrWhiteSpace(st) True
Basic String.Concat(str1, str2) Ui,Path UiPath
Basic String.Join(“,”, arrString) {‘A’,‘B’,‘C’} A,B,C
Substring st.Substring(0,5) Invoice123 Invoi
Substring st.Substring(st.Length - 3) Account123 123
Substring st(0) ABC A
Substring st.Last Test t
Substring st.IndexOf(“abc”) xyzabc123 3
Substring st.LastIndexOf(“x”) index x match x 14
Substring st.Split("-"c)(0) 123-456 123
Substring st.Remove(0,3) ABCDEF DEF
Substring st.Substring(st.IndexOf(“(”)+1, st.IndexOf(“)”) - st.IndexOf(“(”) -1) Item(ID:12345) 12345
Substring Regex.Match(st, “\d{6}”).Value OTP: 123456 123456

Example:

Category Example Sample Input Expected Output
Replace st.Replace(“old”,“new”) old text new text
Replace Regex.Replace(st,“\s+”," ") a b c a b c
Replace Regex.Replace(st,“[^A-Za-z0-9]”, “”) T!e@x#t$ Text
Replace st.Replace(Environment.NewLine," ") Line1
Line2 Line1 Line2
Replace st.Replace(vbTab, " ") Name Value Name Value
Replace st.Replace(“,”, “”).Replace(“.”, “”) 1,234.56 123456
Replace st.Remove(st.Length - 1) Hello! Hello
Replace st.TrimStart("0"c) 000123 123
Replace Regex.Replace(st,“[^\d]”,“”) AB123CD 123
Replace st.Replace(" “,”").ToUpper Ui Path UiPath
Search st.Contains(“abc”) 123abc456 True
Search st.StartsWith(“2025”) 2025-Q1 True
Search st.EndsWith(“.com”) example.com True
Search st.Equals(“ExactText”) ExactText True
Search st.ToLower.Contains(“admin”) HelloAdmin True
Search Regex.IsMatch(st, “^\d+$”) 12345 True
Search Regex.Match(st,“[A-Z]{3}”).Success ABC123 True
Search st.Any(Function(c) Char.IsDigit(c)) abc9xyz True
Search st.All(Function(c) Char.IsLetterOrDigit(c)) Test123 True
Search Not Regex.IsMatch(st,“[^\w\s]”) Safe_String 123 True

Example:

Category Example Sample Input Expected Output
Format st.PadLeft(10,"0"c) 123 0000000123
Format st.PadRight(10,"_"c) abc abc_______
Format String.Format(“{0:C}”, 123.45) $123.45
Format String.Format(“{0:0000}”, 5) 0005
Format $“{st1}-{st2}” Ui,Path Ui-Path
Format st.ToString(“N2”) 1234.5 1,234.50
Format String.Format(“{0,10}”, st) UiPath UiPath
Format String.Format(“{0,-10}”, st) UiPath UiPath
Format String.Format(“{0:D4}”, 9) 0009
Format String.Format(“{0:MM/dd/yyyy}”, dateVar) 2025-06-01 06/01/2025
LINQ New String(st.Where(Function(c) Char.IsLetter(c)).ToArray) A1B2C3 ABC
LINQ New String(st.Distinct.ToArray) aabbcc abc
LINQ st.Where(Function(c) Char.IsDigit(c)).Count abc123 3
LINQ st.Reverse.ToArray abc {‘c’,‘b’,‘a’}
LINQ New String(st.Where(Function(c) Not Char.IsWhiteSpace(c)).ToArray) a b c abc
LINQ String.Join(“”, st.ToCharArray().Reverse) abc cba
LINQ st.Where(Function(c) Char.IsLetter(c)).FirstOrDefault 123abc a
LINQ st.Count(Function(c) c = "a"c) banana 3
LINQ st.Select(Function(c) Char.ToUpper(c)).ToArray abc {‘A’,‘B’,‘C’}
LINQ String.Concat(st.OrderBy(Function(c) c)) cba abc

Example:

Category Example Sample Input Expected Output
DateTime Now.ToString(“dd-MM-yyyy”) 01-06-2025
DateTime Now.ToString(“yyyyMMdd_HHmmss”) 20250601_153000
DateTime Now.AddDays(7).ToString(“MMMM dd”) June 08
DateTime CDate(“2025-06-01”).ToString(“ddd”) Sun
DateTime Today.ToShortDateString 01/06/2025
DateTime DateTime.Now.ToString(“HH:mm:ss”) 15:30:00
DateTime Now.ToString(“dddd, dd MMMM yyyy”) Sunday, 01 June 2025
DateTime DateTime.ParseExact(“01-06-2025”, “dd-MM-yyyy”, Nothing).ToString(“MM/yyyy”) 06/2025
DateTime DateDiff(DateInterval.Day, Date.Parse(“2024-05-01”), Now).ToString 396
DateTime DateTime.UtcNow.ToString(“yyyy-MM-ddTHH:mm:ssZ”) 2025-06-01T10:00:00Z
Misc Guid.NewGuid.ToString d4b7e85f-1b01-4f6b-a6f4-4cdbb4f02db8
Misc Convert.ToString(Nothing)
Misc If(st.Contains(“Error”),“Failed”,“Success”) No Error Found Success
Misc st.Insert(3,“X”) ABCD ABCXD
Misc Microsoft.VisualBasic.Strings.StrReverse(st) ABCD DCBA
Misc System.IO.Path.GetFileName(path) C:\Users\file.txt file.txt
Misc System.IO.Path.GetExtension(path) C:\Users\file.txt .txt
Misc Uri.EscapeDataString(st) Raj Kumar Raj%20Kumar
Misc HttpUtility.HtmlEncode(st)
<div>
Misc st.Normalize(System.Text.NormalizationForm.FormD) naïve naïve

Example:

Category Example Sample Input Expected Output
Regex Regex.Match(st, (?<=ID:)\d+).Value User ID:12345 12345
Regex Regex.Matches(st, \b\w{5}\b) Apple Mango Grape {‘Apple’,‘Mango’,‘Grape’}
Regex Regex.Match(st,(?<=Start)(.*)(?=End)).Value StartMiddleEnd Middle
Regex Regex.Replace(st,\b[A-Z]{2,}\b,) HELLO test test
Regex Regex.Match(st, \d{2}/\d{2}/\d{4}).Value Date: 01/06/2025 01-06-2025
Regex Regex.Matches(st, [A-Z][a-z]+).Cast(Of Match).Select(Function(m) m.Value).ToArray John Doe {‘John’,‘Doe’}
Regex Regex.Match(st, [A-Z]{2}\d{6}).Value PAN: AB123456 AB123456
Regex Regex.Replace(st,[A-Z],) AbC123 b123
Regex Regex.Match(st,(?<=Rs. )\d+).Value Amount Rs. 5000 5000
Regex Regex.Match(st,(?<=Name: ).*?(?=, Age)).Value Name: Raj, Age: 30 Raj
Array arrString.Contains(value) {‘key’,‘value’} TRUE
Array String.Join(, arrString.OrderBy(Function(x) x)) {‘z’,‘a’,‘m’} a,m,z
Array arrString.Distinct.ToArray {‘a’,‘b’,‘a’} {‘a’,‘b’}
Array arrString.Where(Function(x) x.Contains(INR)).ToArray {‘100 INR’,‘USD 200’} {‘100 INR’}
Array arrString.Count(Function(x) x.StartsWith(A)) {‘Apple’,‘Banana’,‘Avocado’} 2
Array String.Join(Environment.NewLine, arrString) {‘One’,‘Two’} One
Two
Array arrString.Skip(1).Take(3).ToArray {‘A’,‘B’,‘C’,‘D’} {‘B’,‘C’,‘D’}
Array arrString.First(Function(x) x.Length > 5) {‘Cat’,‘Animal’,‘Giraffe’} Animal
Array arrString.All(Function(x) x.Contains(Valid)) {‘ValidID’,‘ValidCode’} TRUE
Array arrString.Aggregate(Function(a,b) a + + b) {‘One’,‘Two’,‘Three’}

Example:

7 Likes