Add, Remove, Replace String In C#

Code examples to add strings in C#, remove string in C#, and replace string in C#.

Add or Concatenate Strings In C#

 
Adding strings is also known as concatenating strings. We can add two strings in C# by simpy using the + operator. Here is a detailed article on this, 6 Effective Ways To Concatenate Strings In C#
 
We can also insert a string at a specified positon in another string. String.Insert method inserts a specified string at a specified index position in an instance. For example, the following code inserts "bbb" after the second character in str1 and the result string is "pbbbpp".
  1. string str1 = "ppp";  
  2. string strRes = str1.Insert(2, "bbb");  
  3. Console.WriteLine(strRes.ToString());  

String Interpolation is the most effective way to add strings. Learn more here, Understanding String Interpolation in C#.

Remove String In C#

 
String.Remove() method removes a given number of characters from a string at a specified position. The position is a 0 index position. That means the 0th position is the first character in the string.

In C#, Strings are immutable. That means the method does not remove characters from a string. The method creates and returns a new string without those characters.

String.Remove() method has two overloaded forms.

Method Description
Remove(Int32) Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.
Remove(Int32, Int32) Returns a new string in which a specified number of characters in the current instance beginning at a specified position has been deleted.

Example 1

The following example removes all characters from a string that are after 25th position in the string.

  1. string founder = "Mahesh Chand is a founder of C# Corner";  
  2. // Remove all characters after first 25 chars  
  3. string first25 = founder.Remove(25);  
  4. Console.WriteLine(first25);  

The following example removes 12 characters from the 10th position in the string.

  1. // Remove characters start at the 10th position, next 12 characters  
  2. String newStr = founder.Remove(10, 12);  
  3. Console.WriteLine(newStr);  

Example 2

Now, let’s say you want to delete everything after or before a substring in a string. We can use String.IndexOf() to find the position of the substring and can use the starting index and number of characters to remove.

The following example removes everything before and after substring ‘founder’ in a string.

  1. // Remove everything after founder  
  2. int pos = founder.IndexOf("founder");  
  3. if (pos >= 0) {  
  4.     // String after founder  
  5.     string afterFounder = founder.Remove(pos);  
  6.     Console.WriteLine(afterFounder);  
  7.     // Remove everything before founder but include founder  
  8.     string beforeFounder = founder.Remove(0, pos);  
  9.     Console.Write(beforeFounder);  
  10. }  

The complete program is listed in Listing 1.

  1. using System;  
  2. namespace RemoveStringSample {  
  3.     class Program {  
  4.         static void Main(string[] args) {  
  5.             /** Remove sample **/  
  6.             string founder = "Mahesh Chand is a founder of C# Corner";  
  7.             // Remove all characters after first 25 chars  
  8.             string first25 = founder.Remove(25);  
  9.             Console.WriteLine(first25);  
  10.             // Remove characters start at 10th position, next 12 characters  
  11.             String newStr = founder.Remove(10, 12);  
  12.             Console.WriteLine(newStr);  
  13.             int pos = founder.IndexOf("founder");  
  14.             if (pos >= 0) {  
  15.                 // String after founder  
  16.                 string afterFounder = founder.Remove(pos);  
  17.                 Console.WriteLine(afterFounder);  
  18.                 // Remove everything before founder but include founder  
  19.                 string beforeFounder = founder.Remove(0, pos);  
  20.                 Console.Write(beforeFounder);  
  21.             }  
  22.             Console.ReadKey();  
  23.         }  
  24.     }  
  25. }  

Listing 1.

 

Replace Strings In C# 

 

String.Replace() method replaces a character or a string with another character or string in a string. 

In C#, Strings are immutable. That means the method does not replace the characters or strings from a string. The method creates and returns a new string with new characters or strings.

String.Replace() method has two overloaded forms.

Method Description
Replace(Char, Char) Returns a new string in which all occurrences of a specified Unicode character in this instance are replaced with another specified Unicode character.
Replace(String, String) Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

Example 1

The following example replaces all commas with a colon in a string.

  1. // Replace a char  
  2. string odd = "1, 3, 5, 7, 9, 11, 13, 15, 17, 19";  
  3. Console.WriteLine($ "Original odd: {odd}");  
  4. string newOdd = odd.Replace(','':');  
  5. Console.WriteLine($ "New Odd: {newOdd}");  

The following example replaces all “Beniwal” with an empty string so only first names are copied to the new string.

  1. string authors = "Mahesh Beniwal, Neel Beniwal, Raj Beniwal, Dinesh Beniwal";  
  2. Console.WriteLine($ "Authors with last names: {authors}");  
  3. // Remove all Beniwal with space and remove space with empty string  
  4. string firstNames = authors.Replace(" Beniwal""");  
  5. Console.WriteLine($ "Authors without last names: {firstNames}");  

The complete program is listed in Listing 1.

  1. using System;  
  2. namespace ReplaceStringSample {  
  3.     class Program {  
  4.         static void Main(string[] args) {  
  5.             /** Replace sample **/  
  6.             // Replace a char  
  7.             string odd = "1, 3, 5, 7, 9, 11, 13, 15, 17, 19";  
  8.             Console.WriteLine($ "Original odd: {odd}");  
  9.             string newOdd = odd.Replace(','':');  
  10.             Console.WriteLine($ "New Odd: {newOdd}");  
  11.             string authors = "Mahesh Beniwal, Neel Beniwal, Raj Beniwal, Dinesh Beniwal";  
  12.             Console.WriteLine($ "Authors with last names: {authors}");  
  13.             // Remove all Beniwal with space and remove space with empty string  
  14.             string firstNames = authors.Replace(" Beniwal""");  
  15.             Console.WriteLine($ "Authors without last names: {firstNames}");  
  16.             Console.ReadKey();  
  17.         }  
  18.     }  
  19. }  

Listing 1.

The output of Listing 1 looks like Figure 1.

C# String Replace
Figure 1.
 
The Split method separates strings by a specified set of characters and places these strings into an array of strings. For example, the following source code splits strArray based on ',' and stores all separated strings in an array.
  1. string str1 = "ppp";  
  2. string str2 = "ccc";  
  3. string str3 = "kkk";  
  4. string strAll3 = str1 + ", " +str2+", "+str3 ;  
  5. string[] strArray = strAll3.Split(',');  
  6. foreach (string itm in strArray)  
  7. {  
  8.    Console.WriteLine(itm.ToString() );  
  9. }  
The output from the above code is shown below
 
c# string 
 
To learn more about C# strings, check out String In C# tutorial to learn more about strings and how to work with strings in C#.