STRINGS (SUBSTRING) IN C#

Today in Engineer's World, we are discussing a very important topic of C# -String in very easy way. Posted By- +Manish Kumar Gautam +LIV WIRE +ASP.NET SOLUTIONS

Click imaginationhunt.blogspot to see latest Blogs
 
To read STRINGS (SUBSTRING) IN C# Part - 1 - Click here.



We have learned till now how to use the Substring() method. Now, it's time to learn and read about various other methods and properties that can generally be used with string in C# and also how to use them with String.Substring() methods. 

The most common ways by which we can manipulate string are mentioned below:

1. IndexOf()
IndexOf() method is used to search the position of the specified character from the given string. Its return type is Integer.

Example- Suppose we have a string that has four fields name, education, mobile and email and we need to search the position of B?

Practical Implementation:

using System;

namespace
AspnetSolutions
 {
    class Substring
    {
        public static void
Main()
         {
            string str = "Name=Tapan,Education=BCA,Mb=xxxxxxx,Email=Tap@tap.com";

            Console.WriteLine("Indexof()");
            //Using Indexof()
            Console.WriteLine("Using String: " + str.IndexOf("B"));
            Console.WriteLine("Using Substring: " + str.Substring(str.IndexOf("B"), 3));

        }
    }
}

Click imaginationhunt.blogspot to see latest Blogs

Output

FIGURE 1

In the output window, we have two different outcomes. Let's understand each one of them.
a) Using String- First one shows how we can search the index of 'B' from the given string. 
b) Using Substring- Second one shows how we can make use of indexof() in substring to get a sub part of the given string. For ex- fetching out "BCA" by using the index 'B'.

2. LastIndexOf()

LastIndexOf() method is used to search the last position of the specified character from the given string. Its return type is Integer.

Example- Suppose we have a string that has four fields name, education, mobile and email and we need to search the last occurrence of character 'T'?

Practical Implementation:

using System;

namespace
AspnetSolutions 
{
    class Substring
    {
        public static void
Main() 
        {
            string str = "Name=Tapan,Education=BCA,Mb=xxxxxxx,Email=Tap@tap.com";

            Console.WriteLine("LastIndexOf()");
            //Using LastIndexOf()
            Console.WriteLine("Using String: " + str.LastIndexOf("T"));
            Console.WriteLine("Using Substring: " + str.Substring(str.LastIndexOf("T")));

        }
    }
}

Output

FIGURE 2

Click imaginationhunt.blogspot to see latest Blogs

In the output window, we have two different outcomes. Let's understand each one of them.
a) Using String- First one shows the last occurrence of 'T' from the given string. 
b) Using Substring- Second one shows how we can make use of indexof() in substring to get a sub part of the given string. For ex- fetching out email value by using the index 'T'.
Related Questions:
 
#newtoprgm try firstprogram

Q-1 What is the output of the following code?

using System;

namespace AspnetSolutions
{
    class Substring
    {
        public static void
Main() 
        {
            string line = "A quick brown fox jump Over a lazy dog";

            Console.WriteLine(line.IndexOf("o"));
            Console.WriteLine(line.IndexOf("O"));

        }
    }
}



A) Both are returning the same index
B) Different index for both variables
Ans- Option(B). 
Explanation: Because the ASCII code of o=111 and O=79 are different.

Q-2 What is the output of the following code?

using System;

namespace AspnetSolutions 
{
    class Substring
    {
        public static void
Main() 
        {
            string email = "Tapan Email= Tap@Tap.com";

            Console.WriteLine(email.IndexOf("T"), email.LastIndexOf("T"));

        }
    }
}


Click imaginationhunt.blogspot to see latest Blogs
 
A) 0, 17
B) 0, 13
C) Error
Ans- Option(C), Error.
Explanation: Error: The best overloaded method match for 'System.Console.WriteLine(string, object)' has some invalid arguments.

Keep learning and sharing...

Comments

Popular posts from this blog

SSC CGL 2016 Tier-1 Marks / Result

DATA TYPES IN C#

Pattern printing program

IS STRING MUTABLE OR IMMUTABLE IN .NET?

Top Mutual Funds

SSC CGL 2017 TIER 2 RESULT OUT

JUST IN TIME (JIT)

Can we have multiple Main methods in one class

TOP 15 VISUAL STUDIO SHORTCUT KEYS

Top 10 Interview Tips and Questions