Showing posts with label Strings. Show all posts
Showing posts with label Strings. Show all posts

String Function : Substring

Substring of a string

To extract the substring of a string we can make use of the method substring()

This method has two overloaded constructors.
Substring(int startIndex) this will take the starting index of the substring to be returned for example if we choose exampleString.Substring(4) , all the characters starting from the index 4 till the end of the string is returned as a sub string.

Substring(int startIndex, int length) this will  take the starting index of the substring an the count of the characters from the starting index to be returned 
for example if we choose exampleString.Substring(4, 5) , all the characters starting from the index 4 till the (4+5)th index of the string is returned as a sub string.


The Code is as below 

using System;
namespace Logiphix
{
    class Program
    {
        static void Main(string[] args)
        {
            string s;

            s = "Substring of this string";
            Console.WriteLine(s);
            Console.WriteLine(s.Substring(3)); //// this will give the string that starts from the index 3
            Console.WriteLine(s.Substring(3, 6)); //// string from the index 3 till a length of 5 characters 

            Console.ReadKey();
        }
    }
}

The output is as below:

String functions: Trimming a string

Trimming a string.
You must have always come across a situation where you may need to trim a string in order to attain a specific result or to use the string for a purpose which need a part of the string trimmed from either the start or the end of the string.

C# provides a specific function just  for this purpose to make your life easy.

The System.String class is specifically for storing the string objects. and has a lot of built-in methods defined to help the programmer to perform operations on the strings.

The Trim() method is used to remove the white spaces from a string from the leading or the end of the string.
when we apply this method on any string , any leading or trailing white spaces are been trimmed and the string is returned.

The Trim has a overloaded method that takes a character array as an input parameter.
Hence Trim('a','b','c') will trim any appearance of the character a or b or c in the leading or the trailing end of the string.

The TrimStart('a',b','c') will trim the string of the characters only which occur at the start of the string and TrimEnd('a','b') will trim the trailing end of the string only.

Note: all the trim functions mentioned above return a string value. Hence their return type is string.


The code is as follows:


using System;
namespace Logiphix
{
    class Program
    {
        static void Main(string[] args)
        {
            string s =  "  This string will be trimmed  ";
            Console.WriteLine(s);

            s = s.Trim(); //// This will remove all the Leading and trailing whitespaces from the string
            Console.WriteLine(s);

            s = s.Trim('T'); //// This will remove all the Leading and trailing character 'i' appearances 
            Console.WriteLine(s);

            s = s.TrimStart('h'); //// This will remove all the Leading occourace of the character 'h' 
            Console.WriteLine(s);

            s = s.TrimEnd('d'); //// This will remove all  the trailing character 'd' from the string
            Console.WriteLine(s);
            Console.ReadKey();
        }
    }
}


The output will be :


Please note that the previous trimming is retained even in the next trim , this is because we store the value in s each time we make the operation hence s gets a new trimmed string value in it each time.