C#.Net Strings

A string is a collection of characters. In C#, String is a reference data type, the benefit of reference data type is that it can reduce or increase the size of string at runtime.

String built-in methods

Example 1: Input string and find its length.

class Program
{
    static void Main(string[] args)
    {
        int len;
        String name;
        Console.WriteLine("Enter name");
        name = Console.ReadLine();
        len = name.Length;
        Console.WriteLine("Length = " + len);
        Console.ReadLine();
    }
}

Example 2: Input string and convert into lowercase and uppercase

static void Main(string[] args)
{
    string s;
    Console.WriteLine("Enter string: ");
    s = Console.ReadLine();
    Console.WriteLine("String in lowercase is " + s.ToLower());
    Console.WriteLine("String in uppercase is " + s.ToUpper());
    Console.ReadLine();
}

Example 3: Concatenate 2 strings

//input 2 numbers in string and sum them
static void Main(string[] args)
{
    string s1, s2, s3;
    Console.WriteLine("Enter 2 strings");
    s1 = Console.ReadLine();
    s2 = Console.ReadLine();
    s3 = s1 + s2; //concatenate (merge, join)
    Console.WriteLine("Sum = " + s3);
    
    Console.ReadLine();
}

Example 4: Check string is starting or ending with a particular string or not.

static void Main(string[] args)
{
    string a = "New Delhi";
    string b = "New York";
    string c = "Old City";
    
    Console.WriteLine(a.StartsWith("new"));  //true
    Console.WriteLine(b.StartsWith("new"));  //true
    Console.WriteLine(c.StartsWith("new"));  //false

    Console.WriteLine(a.EndsWith("Delhi"));  //true
    Console.WriteLine(b.EndsWith("Delhi"));  //false
    Console.WriteLine(c.EndsWith("Delhi"));  //false
    
    Console.ReadLine();
}

Example 5: Find character position using IndexOf

static void Main(string[] args)
{
    string s;
    int i;
    Console.WriteLine("Enter string: ");
    s = Console.ReadLine();
    i = s.IndexOf('a');
    //i = s.IndexOf("hi");
    //i = s.IndexOf('e',3);
    //i = s.IndexOf("prg", 2);
    Console.WriteLine("Index of a is " + i);
    Console.ReadLine();
}

Example 6: Replace a character or string.

static void Main(string[] args)
{
    string s;
    Console.WriteLine("Enter string: ");
    s = Console.ReadLine();
    s = s.Replace(" ", ",");
    Console.WriteLine("After replacing new string is: " + s);
    Console.ReadLine();
}

Example 7: Insert a character or string in between a string

static void Main(string[] args)
{
    string s;
    Console.WriteLine("Enter string: ");
    s = Console.ReadLine();
    s = s.Insert(5, "!");
    Console.WriteLine("After inserting new string is: " + s);
    Console.ReadLine();
}

Example 8: Remove a character or string in between the string.

static void Main(string[] args)
{
    string s;
    Console.WriteLine("Enter string: ");
    s = Console.ReadLine();
    s = s.Remove(2);
    //s = s.Remove(2,1);
    Console.WriteLine("After removing string is: " + s);
    Console.ReadLine();
}

Example 9: Input string and return substring.

static void Main(string[] args)
{
    String s1, s2;
    Console.WriteLine("Enter name");
    s1 = Console.ReadLine();
    s2 = s1.Substring(2); // start from length 2
    //s2 = s1.Substring(2,3); //start length 2, no of char 3
    Console.WriteLine("New String = " + s2);
    Console.ReadLine();
}

Example 10: Find vowel from input string using ToCharArray() method.

static void Main(string[] args)
{
    string s;
    int count=0;
    Console.WriteLine("Enter string: ");
    s = Console.ReadLine();
    char []ch = s.ToCharArray();
    foreach(char c in ch)
    {
        if(c=='a' || c=='e' || c=='i' || c='o' || c=='u')
        {
            count++;
        }
    }
    Console.WriteLine("Total vowels are : " + count);
    Console.ReadLine();
}

Example 11: Compare 2 string and find which is greater.

static void Main(string[] args)
{
    string a = "samesh";
    string b = "ramesh nagar";
    
    //int i = a.CompareTo(b);
    int i = string.Compare(a, b); //same as above statment
    
    //Console.WriteLine("Value = " + i);
    /*Above statement will print value: 
    * print 0 if both r equal
    * print -1 if 2nd str is greater
    * print 1 if 1st str is greater.
    */
    
    if(i==0)
    {
        Console.WriteLine("Both are equal");
    }
    else
    if(i<0)
    {
        Console.WriteLine("2nd str is greater");
    }
    else
    {
        Console.WriteLine("1st str is greater");
    }
    
    Console.ReadLine();
}

Example 12: Trim leading and trailing characters

static void Main(string[] args)
{
    string s;
    Console.WriteLine("Enter string: ");
    s = Console.ReadLine();
    Console.WriteLine("Before trim string is:" + s);
    s = s.Trim(' ');
    //s = s.Trim('#');
    //s = s.Trim('a');
    Console.WriteLine("After trim string is:" + s);
    Console.ReadLine();
}

Example 13: PadLeft & PadRight functions

static void Main(string[] args)
{
    string s;
    Console.WriteLine("Enter string: ");
    s = Console.ReadLine();
    Console.WriteLine("Before PadLeft string is:" + s);
    s = s.PadLeft(20, '*');
    //s = s.PadRight(20, '*');
    Console.WriteLine("After PadLeft string is:" + s);
    Console.ReadLine();
}

Example 14: tryparse functions

static void Main(string[] args)
{
    string a = "Moti nagar";
    string b = "Ramesh nagar";
    string creditcard = "7250";
    int i;
    
    Console.WriteLine(Int32.TryParse(a, out i));
    Console.WriteLine(Int32.TryParse(b, out i));
    Console.WriteLine(Int32.TryParse(creditcard, out i));
    
    if(Int32.TryParse(a, out i))
    {
        Console.WriteLine(Convert.ToInt32(a)*2);
    }
	
    if (Int32.TryParse(b, out i))
    {
        Console.WriteLine(Convert.ToInt32(b) * 2);
    }
    
    if (Int32.TryParse(creditcard, out i))
    {
        Console.WriteLine(Convert.ToInt32(creditcard) * 2);
    }
    
    Console.ReadLine();
}
Exercise Questions: