Thursday, May 30, 2013

String handling in C#

What is string ?
A string is a sequence of characters. In C#, a string is a sequence of unicode characters. It is a data type which stores a sequence of data values, usually bytes, in which elements usually stand for characters according to a character encoding. When a string appears literally in source code, it is known as a string literal.

How to display string ?
It’s very easy to display string in c# . In below code I have given example.
class Program
    {
        static void Main(string[] args)
        {
            string name = "Sourav Kayal";  //Initialization of string
            Console.Write(name);
            Console.Read();
        }
    }
Here name is a string variable and in am initializing string by “=” operator. And I am using string variable name as a argument of Write() function. Like this,
            Console.Write(name);
And the output is here.







 Get length of string.
String class contains Length property , and using that you can get length of string variable. Here below code will show length of string
class Program
    {
        static void Main(string[] args)
        {
            string name = "Sourav";
            Console.Write(name.Length);  // Length property gives length of string
            Console.Read();
        }
    }

And the output is here





Concatenation of  string in c#
It’s very easy to concatenate two and more string in c#. Here ‘+’ operator is overloaded and perform concatenation operation. Here I have given below sample code.
class Program
    {
        static void Main(string[] args)
        {
            string name = "Sourav";
            string surname = " Kayal";
            Console.Write(name + surname);
            Console.Read();
        }
    }
And the output is here.


Convert to uppercase and lowercase.
It’s very natural need to convert lower case string to upper case and vice versa. String class provides toUpper() and toLowe function to get it done. Have a look to below code.
class Program
    {
        static void Main(string[] args)
        {
            string name = "sourav";
            Console.Write(name.ToUpper());  //Make lower case to uppercase

            string SURNAME = "KAYAL";
            Console.Write(SURNAME.ToLower()); //Make upper to lower case

            Console.Read();
        }
    }
This is the output.


Get Sub string from a long string
String class provides SubString() function to get part of string. It takes two argument
1)      Starting character.
2)      Length of character.
Like
Substring(0,3)
Here 0 indicates 0th character means first character. And 3 represent three character from the first character. I have given the code snippet here.

class Program
    {
        static void Main(string[] args)
        {
            string name = "sourav";
            Console.Write(name.Substring(0,3));

            Console.Read();
        }
    }
And the output is


Split long string to string array
If you have a lengthy string separated by some special character, then use split() function to make them separate string each.
Here is the code for them
class Program
    {
        static void Main(string[] args)
        {
            string name = "name1,name2,name3";
            string[] Each_name = name.Split(',');   //Spiliting string by , separator.
            Console.Write(Each_name[1]);
            Console.Read();
        }
    }


And the output is




No comments:

Post a Comment