String Manipulation in C#

Suppose you want to reverse a string. This article shows how to do that.

The first way uses the reverse function.

  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             string s = string.Empty;  
  6.             s = Console.ReadLine();  
  7.             char[] arr = s.ToCharArray();  
  8.             Array.Reverse(arr);  
  9.             Console.WriteLine(arr);  
  10.             Console.ReadLine();  
  11.         }   
  12.     } 

Output

 

The second way uses a loop. We first find the length of the string and loop through in a backward direction.
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             string s = string.Empty;  
  6.             s = Console.ReadLine();  
  7.             char[] arr = s.ToCharArray();  
  8.             char[] arr1 = new char[s.Length];  
  9.             string s1 = string.Empty;  
  10.             for (int i = s.Length - 1; i >= 0; i--)  
  11.             {  
  12.                 s1 += arr[i];  
  13.             }  
  14.             Console.WriteLine(s1);  
  15.             Console.ReadLine();  
  16.         }  

Output



We have now learned how to reverse a string. Okay the next question is, how to check a string to determine whether it is a Palindrome or not.

Note: A string is said to be a Palindrome if the string from left to right is equal to the string from right to left, for example MALAYALAM and so on.
To overcome this we only check the reverse of the string to the original string.
  1. class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             string s = string.Empty;  
  6.             s = Console.ReadLine();  
  7.             char[] arr = s.ToCharArray();  
  8.             char[] arr1 = new char[s.Length];  
  9.             string s1 = string.Empty;  
  10.             for (int i = s.Length - 1; i >= 0; i--)  
  11.             {  
  12.                 s1 += arr[i];  
  13.             }  
  14.             if (s1 == s)  
  15.             {  
  16.             Console.WriteLine("Palindrome String");  
  17.             Console.ReadLine();  
  18.             }  
  19.             else  
  20.             {  
  21.                 Console.WriteLine("Not Palindrome String");  
  22.                 Console.ReadLine();  
  23.             }  
  24.         }  

Output



Now, enter a string and count the number of capital letters, small letters, digits, spaces and special characters.
  1.   class Program  
  2.     {  
  3.         static void Main(string[] args)  
  4.         {  
  5.             string s = string.Empty;  
  6.             s = Console.ReadLine();  
  7.             int number = 0;  
  8.             int capitalLetter = 0;  
  9.             int smallletter = 0;  
  10.             int space = 0;  
  11.             int SpecialSymbol = 0;  
  12.             char[] a = s.ToCharArray();  
  13.             for (int i = 0; i < a.Length; i++)  
  14.             {  
  15.                 if (a[i] >= 48 && a[i] <= 58)  
  16.                 {  
  17.                     number += 1;  
  18.                 }  
  19.                 else if (a[i] >= 65 && a[i] <= 90)  
  20.                 {  
  21.                     capitalLetter += 1;  
  22.                 }  
  23.                 else if (a[i] >= 97 && a[i] <= 122)  
  24.                 {  
  25.                     smallletter += 1;  
  26.                 }  
  27.                 else if (a[i] == 32)  
  28.                 {  
  29.                     space += 1;  
  30.                 }  
  31.                 else  
  32.                 {  
  33.                     SpecialSymbol += 1;  
  34.                 }  
  35.             }  
  36.             Console.Write("Capital Letter              " + capitalLetter);  
  37.             Console.Write("\nsmall letter              " + smallletter);  
  38.             Console.Write("\nnumber are                " + number);  
  39.             Console.Write("\nTotal Spaces              " + space);  
  40.             Console.Write("\nTotal Special Symbol are  " + SpecialSymbol);  
  41.             Console.ReadLine();  
  42.         }  

Output



Now I will explain how to compare strings using the compare function.

String.compare(string,string2,[opyional[true|false])

This function returns 0 if both strings are equal.

It returns -1 if string2 is greater than string1 and returns 1 if string1 is greater than string2.

It takes an optional parameter that is true or false. If we pass true than it ignores case for the comparison.

Example
  1. string s1 = "NANhE";  
  2. string s2 = "NANHe";  
  3. int n = string.Compare(s1, s2, true);  
  4. //int n = s1.CompareTo(s2);  
  5. Console.Write(n);  
  6. Console.ReadLine(); 
Output: 0

If
  1. string s1 = "NANhE";  
  2. string s2 = "NANHe";  
  3. int n = string.Compare(s1, s2, false);  
  4. //int n = s1.CompareTo(s2);  
  5. Console.Write(n);  
  6. Console.ReadLine(); 
Output: -1

By default it is false.

Now the next string function is the string.contains function.
  1. string s1 = "The quick brown fox jumps over the lazy dog";  
  2. string s2 = "lox";  
  3. bool b;  
  4. b = s1.Contains(s2);  
  5. Console.Write(b);  
  6. Console.ReadLine(); 
If the s2 string is found inside the s1 string then this function returns true otherwise false.

Many string functions exist but it is impossible to cover all in this article.

Up Next
    Ebook Download
    View all
    Learn
    View all