We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Friday, April 27, 2018

String manipulation in c#

How to give backslash(\) in string
  
static void Main(string[] args)
        {
            string strSlas = "Go to your c:\\ drive";

            Console.WriteLine(strSlas);
            Console.ReadLine();

        }
Output:



How to give double quote inside the string

static void Main(string[] args)
        {
            string str= "Welcome! \"And how\" are you?";

            Console.WriteLine(str);
            Console.ReadLine();

        }

Output:


How to display in a new line of string

static void Main(string[] args)
        {
            string str= "I am fine \n And how are you?";

            Console.WriteLine(str);
            Console.ReadLine();

        }
Output:


String Replacement by index

static void Main(string[] args)
        {
            string str = string.Format("{0}!", "Welcome");

            Console.WriteLine(str);
            Console.ReadLine();

        }
Output:


 String substitution
  
static void Main(string[] args)
        {
            string str = string.Format("Make: {0} (Model: {1})", "Maruti","TS 0921");

            Console.WriteLine(str);
            Console.ReadLine();

        }

Output:

Currency Display

static void Main(string[] args)
        {
            string str = string.Format("{0:C}", 123.45);

            Console.WriteLine(str);
            Console.ReadLine();

        }
Output:


Display decimal point

static void Main(string[] args)
        {
            string str = string.Format("{0:N}", 1234567890);

            Console.WriteLine(str);
            Console.ReadLine();

        }

Output:


Display in percentage sign
  
static void Main(string[] args)
        {
            string str = string.Format("{0:P}",.654);

            Console.WriteLine(str);
            Console.ReadLine();

        }

Output:


Display in phone number format

static void Main(string[] args)
        {
            string str = string.Format("Phone number: {0:(###) ###-###}",9884531292);

            Console.WriteLine(str);
            Console.ReadLine();

        }
Output:



String concatenate

static void Main(string[] args)
        {
            string str = "";
            for (int i = 0; i < 100; i++)
            {
                str += "--" + i.ToString();
            }

            Console.WriteLine(str);
            Console.ReadLine();

        }

Output:


Use substring

static void Main(string[] args)
        {
            string str = "Hi friends how are you.";
            str = str.Substring(10,8);

            Console.WriteLine(str);
            Console.ReadLine();

        }

Output: 


Display in upper case

static void Main(string[] args)
        {
            string str = "Hi friends how are you.";
     

            Console.WriteLine(str.ToUpper());
            Console.ReadLine();

        }
Output:


Calculate the length of string and use trim method for remove space  

static void Main(string[] args)
        {
            string str = "  Hi friends how are you.     ";
            str = string.Format("Total Length: {0} \nAfter remove space total length: {1}", str.Length, str.Trim().Length);

            Console.WriteLine(str);
            Console.ReadLine();

        }
Output:


No comments: