Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 25 June 2015

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
 



3. ToLower()
ToLower() method is used to convert the specified character or string to lowercase from the given string. Its return type is string.

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

Practical Implementation:



namespace AspnetSolutions
{
    class Substring
    {
        public static void
Main() 
        {
            string str = "Name=Tapan,Education=BCA,Mb=xxxxxxx,Email=Tap@tap.com";
            Console.WriteLine("ToLower()");
            //Using ToLower()
            Console.WriteLine("Using String: " + str.ToLower());
          Console.WriteLine("Using Substring: " + str.Substring(str.LastIndexOf("T")).ToLower());
        }
    }
}

Output

FIGURE 1



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 lowercase the given string. 
b) Using Substring- Second one shows how we can get a sub part of the given string using substring() method and display them in lowercase. For ex- fetching out 'Tap@tap.com' and lowering its case to make it 'tap@tap.com'.


4. ToUpper()
ToUpper() method is used to convert the specified character or string to uppercase from the given string. Its return type is string.

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

Practical Implementation:

namespace
AspnetSolutions
{
    class Substring
    {
        public static void
Main() 
         {
            string str = "Name=Tapan,Education=BCA,Mb=xxxxxxx,Email=Tap@tap.com";
            Console.WriteLine("ToUpper()");
            //Using ToUpper()
            Console.WriteLine("Using String: " + str.ToUpper());
          Console.WriteLine("Using Substring: " + str.Substring(str.LastIndexOf("T")).ToUpper());
        }
    }
}

Click imaginationhunt.blogspot to see latest Blogs

Output

FIGURE 2

In the output window, we have two different outcomes. Let's understand each one of them.
a) Using String- First one uppercase the given string. 
b) Using Substring- Second one shows how we can get a sub part of the given string using substring() method and display them in uppercase. For ex- fetching out 'Tap@tap.com' and upper casing its character to make it 'TAP@TAP.COM'.



Related Questions: 


Q-1 What will be the code to get the output? 


Ans- There are many ways to get the output. But one possible solution based on what we have studied in strings till now is mentioned below:

{
    class Substring
    {
        public static void
Main()  
        {
            string emailAddress = "ImagiNaTiOnHuNT.BlogSpOT@asd.in";
            Console.WriteLine("Email Address = " + emailAddress);

            string holdername = emailAddress.Substring(0, emailAddress.LastIndexOf("T") + 1);
            string domainpart = emailAddress.Substring(emailAddress.IndexOf("@") + 1);
            Console.WriteLine("HolderName = {0} \nDomainPart = {1}", holdername.ToUpper(), domainpart.ToLower());
        }
    }
}


Q-2 Suppose we are not using the System Namespace then which of them will result in error?

//using System;                ------------------->Commenting out System namespace
namespace
AspnetSolutions
{
    class Substring
    {
        public static void
Main()  
        {
            string Abc ;
            String Xyz ;
        }
    }
}

Click imaginationhunt.blogspot to see latest Blogs

A) Both having error, string Abc and String Xyz.
B) Both run perfectly, string Abc and String Xyz.
C) Only string Abc
D) Only String Xyz
Ans- Option (D).

Keep learning and sharing...

No comments:

Post a Comment

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...