String Class in C#

Introduction:

System.String is a class specifically designed to store a string and allow a large number of operations on the string. In addition, due to the importance of this data type, C# has its own keyword and associated syntax to make it particularly easy to manipulate strings using this class.

String methods:

  • Compare:

    Compare() method compares the contents of strings, taking into account the in accessing equivalence between certain characters.Compare() methods returns integer.

    string text1 = "Hello";
    string text2 = "World";
    int i = string.Compare(text1, text2);
    if (i > 0)
    {
      Console.WriteLine("Two string are same");
    }
    else
    {
      Console.WriteLine("Two string are different");
    }
    
    Output
    Two string are different
  • CompareOrdinal:

    CompareOrdinal() are same as Compare() but doesn't take culture into account.

    string text1 = "Hello";
    string text2 = "World";
    int i = string.CompareOrdinal(text1, text2);
    if (i > 0)
    {
      Console.WriteLine("Two string are same");
    }
    else
    {
      Console.WriteLine("Two string are different");
    }
    
    Output
    Two string are different
  • Concat:

    Concat () function is used when we combining more than one string into a single string.

    string text1 = "Hello";
    string text2 = "World";
    string text3 = string.Concat(text1, text2);
    Console.Write(text3);
    
    Output
    HelloWorld
  • CopyTo:

    CopyTo() method copies a specific number of characters from the selected index to an entirely new instance of an array.

    string text2 = "World";
    char[] des=new char[10];
    text2.CopyTo(0, des, 0, 3);
    Console.Write(des);
    
    Output
    Wor
  • Format:

    Formats a string containing various values and specifiers for how each value should be formatted.

    Decimal pricePerOunce = 17.36m;
    String s = String.Format("The current price is {0} per ounce.",pricePerOunce);
    Console.WriteLine(s);
    
    Output
    The current price is 17.36 per ounce.
  • IndexOf:

    Locates the first occurrence of a given substring or character in the string.

    string text = "Hello";
    Console.Write(text.IndexOf('o'));
    
    Output
    4
  • IndexOfAny:

    Locates the first occurrence of any one of a set of characters in a string.

    string text = "Hello";
    char[] i = { 'H', 'o' };
    Console.Write(text.IndexOfAny(i,0));
    
    Output
    0
  • Insert:

    Inserts a string instance into another string instance at a specified index.

    string text = "Hello";
    Console.Write(text.Insert(5, " World"));
    
    Output
    Hello World
  • Join:

    Builds a new string by combining an array of strings.

    string[] text = { "Hello", "World", "Welcome" };
    Console.Write(string.Join(" ", text));
    
    Output
    Hello World Welcome
  • LastIndexOf:

    Same as IndexOf but finds the last occurrence.

    string text = "Hello";
    Console.Write(text.LastIndexOf('l'));
    
    Output
    3
  • LastIndexOfAny:

    Same as IndexOfAny but finds the last occurrence.

    string text = "Hello";
    char[] i = { 'l', 'H' };
    Console.Write(text.LastIndexOfAny(i,2));
    
    Output
    2
  • Replace:

    Replaces occurrences of a given character or substring in the string with another character or substring.

    string text = "Hello";
    Console.Write(text.Replace("Hello","World"));
    
    Output
    World
  • Split:

    Splits the string into an array of substrings; the breaks occur wherever a given character occurs.

    string text = "Hello,World";
    string[] arr = text.Split(',');
    foreach (string c in arr)
    {
     Console.Write(c);
    }
    
    Output
    HelloWorld
  • Substring:

    Retrieves the substring starting at a specified position in a string.

    string text = "Hello";
    Console.Write(text.Substring(2));
    
    Output
    llo
  • ToLower:

    Converts string to lowercase.

    string text = "HeLlO";
    Console.Write(text.ToLower());
    
    Output
    hello
  • ToUpper:

    Converts string to uppercase.

    string text = "Hello";
    Console.Write(text.ToUpper());
    
    Output
    HELLO
  • Trim:

    Removes leading and trailing whitespace.

    string text = "***Hello world***";
    string result = text.Trim('*');
    Console.WriteLine(result);
    
    Output
    Hello world


Write a comment


Name Email Message