By 22/07/2015 18 Comments

C# String Class And Methods

  • C# defines a built-in string class which lives inside system namespace such as System.String.
  • String class implements different types of interfaces.
  • It is a sealed class that means we cannot inherit it with other classes.
  • The purpose of string class is that it provides string-handling functionality.
  • There are several built-in overloaded methods available inside a string class which helps us to manipulate and working with string objects in several different ways such as join one string to another, search a specific word in string, make a copy of strings etc.
  • Along with methods the string class also defines a Length property which returns total characters contains a specific string.

Some String Class Methods:

Methods & Description
public static int Compare(string strA, string strB)
Compares two specified System.String objects and returns an integer that indicates their relative position in the sort order.
public static string Concat(string str0, string str1, string str2)
Concatenates three strings.
public bool Contains(string value)
Returns a value indicating whether the specified System.String object occurs within this string.
public static string Copy(string str)
Creates a new instance of System.String with the same value as a specified System.String.
public void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
Copies a specified number of characters from a specified position in this instance to a specified position in an array of Unicode characters.
public bool EndsWith(string value)
Determines whether the end of this string instance matches the specified string.
public static bool Equals(string a, string b)
Determines whether two specified System.String objects have the same value.
public static string Format(string format, object arg0)
Replaces one or more format items in a specified string with the string representation of a specified object.
public System.CharEnumerator GetEnumerator()
Retrieves an object that can iterate through the individual characters in this string.
public override int GetHashCode()
Returns the hash code for this string.
public System.TypeCode GetTypeCode()
Returns the System.TypeCode for class System.String.
public int IndexOf(string value)
Reports the zero-based index of the first occurrence of the specified string in this instance.
public static string Intern(string str)
Retrieves the system’s reference to the specified System.String.
public static bool IsNullOrEmpty(string value)
Indicates whether the specified string is null or an System.String.Empty string.
public static bool IsNullOrWhiteSpace(string value)
Indicates whether a specified string is null, empty, or consists only of white-space characters.
public static string Join(string separator, params string[] value)
Concatenates all the elements of a string array, using the specified separator between each element.
public int LastIndexOf(string value)
Reports the zero-based index position of the last occurrence of a specified string within this instance.
public string PadLeft(int totalWidth)
Returns a new string that right-aligns the characters in this instance by padding them with spaces on the left, for a specified total length.
public string PadRight(int totalWidth)
Returns a new string that left-aligns the characters in this string by padding them with spaces on the right, for a specified total length.
public string Remove(int startIndex, int count)
Returns a new string in which a specified number of characters in this instance beginning at a specified position have been deleted.
public string Replace(string oldValue, string newValue)
Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.
public string[] Split(params char[] separator)
Returns a string array that contains the substrings in this instance that are delimited by elements of a specified Unicode character array.
public bool StartsWith(string value)
Determines whether the beginning of this string instance matches the specified string.
public string Trim()
Removes all leading and trailing white-space characters from the current System.String object.

Example – 1, Use of PadRight() method:

using System;
namespace csharpAdvance
{
    class Program
    {
        static void Main(string[] args)
        {
            // A string initialization.
            string siteName = "Tutorialstown";

            // PadRight() method is called and displayed.
            Console.Write(siteName.PadRight(22, '*'));
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Tutorialstown*********
     */
}


Example – 2, Use of StartsWith() method:

using System;
namespace csharpAdvance
{
    class Program
    {
        static void Main(string[] args)
        {
            // A string initialization.
            string siteName = "Tutorialstown";

            if (siteName.StartsWith("tutorialstown",true, System.Globalization.CultureInfo.CurrentCulture))
                Console.WriteLine("Yes, it is a tutorialstown.com");
            else
                Console.WriteLine("No, it is not a tutorialstown.com");

            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Yes, it is a tutorialstown.com
     */
}


Example – 3, Use of trim() method:

using System;
namespace csharpAdvance
{
    class Program
    {
        static void Main(string[] args)
        {
            // A string initialization.
            string siteName = "\t\t\tTutorialstown";

            Console.WriteLine("Before trim() method:");
            Console.WriteLine(siteName);

            Console.WriteLine("After trim() method:");
            Console.WriteLine(siteName.Trim());

            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Before trim() method:
                               Tutorialstown
        After trim() method:
        Tutorialstown

     */
}


Example – 4, Use of Length property:

using System;
namespace csharpAdvance
{
    class Program
    {
        static void Main(string[] args)
        {
            // A string initialization.
            string siteName = "Tutorialstown";

            Console.WriteLine("Total characters are: {0}", siteName.Length);
            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        Total characters are: 13
     */
}


Example – 5, Use of ToCharArray() method:

using System;
namespace csharpAdvance
{
    class Program
    {
        static void Main(string[] args)
        {
            // A string initialization.
            string siteName = "Tutorialstown";

            // ToCharArray() method converts string into an character array.
            char[] charArray = siteName.ToCharArray();

            // Loop throug each element from char array and displayed.
            for (int index = 0; index < charArray.Length; index++)

                Console.WriteLine(index + " - " + charArray[index]);

            Console.ReadKey();
        }
    }
    /*  The Output will be:
     
        0 - T
        1 - u
        2 - t
        3 - o
        4 - r
        5 - i
        6 - a
        7 - l
        8 - s
        9 - t
        10 - o
        11 - w
        12 - n

     */
}





Next Tutorial →



Posted in: C# Advance, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .