Categories

Tools

bitdefender

1&1 Web Hosting

Use some string manipulation functions with C#


An example of use of the most common functions for manipulating strings in C#.

Code:

using System;
using System.Globalization;
using System.Linq;
using System.Threading;

namespace ConsoleApplications
{
    class StringDemo
    {
        static void Main(string[] args)
        {
            // Set the appropriate culture
            CultureInfo ciEnglish = new CultureInfo("en");
            ciEnglish.DateTimeFormat.Calendar = new GregorianCalendar();
            Thread.CurrentThread.CurrentCulture = ciEnglish;

            // Declare some variables
            String name = "Lisa";
            String company = "MdmSoft";
            double salary = 2800.00d;

            // Print the initial values 
            Console.WriteLine("Name: {0}\nCompany: {1}\nSalary: {2:c}", name, company, salary);
            Console.WriteLine();

            // Use some string manipulation functions
            Console.WriteLine("name.CompareTo(\"LISA\")=" + name.CompareTo("LISA"));
            Console.WriteLine("name.CompareTo(\"Lisa\")=" + name.CompareTo("Lisa"));
            Console.WriteLine("name.CompareTo(\"lisa\")=" + name.CompareTo("lisa"));
            Console.WriteLine();

            Console.WriteLine("name.Contains(\"li\")=" + name.Contains("li"));
            Console.WriteLine("name.Contains(\"Li\")=" + name.Contains("Li"));
            Console.WriteLine();

            Console.WriteLine("name.Count()=" + name.Count());
            Console.WriteLine("company.Count()=" + company.Count());
            Console.WriteLine();

            Console.WriteLine("name.EndsWith(\"sa\")=" + name.EndsWith("sa"));
            Console.WriteLine("company.EndsWith(\"soft\")=" + company.EndsWith("soft"));
            Console.WriteLine("company.EndsWith(\"soft\", true, null)=" + company.EndsWith("soft", true, null));
            Console.WriteLine();

            name = name.Insert(0, "Mrs ");
            Console.WriteLine("name.Insert(0, \"Mrs \")= " + name);
            Console.WriteLine();

            Console.WriteLine("name.Length=" + name.Length);
            Console.WriteLine();

            Console.WriteLine("name.PadLeft(15,'*')=" + name.PadLeft(15, '.'));
            Console.WriteLine();

            Console.WriteLine("name.PadRight(15,'*')=" + name.PadRight(15, '.'));
            Console.WriteLine();

            Console.WriteLine("name.Remove(0,4)=" + name.Remove(0, 4));
            Console.WriteLine();

            name=name.Replace("Lisa", "Sophie");
            Console.WriteLine("name.Replace(\"Lisa\", \"Sophie\")=" + name.Replace("Lisa", "Sophie"));
            Console.WriteLine();

            Console.WriteLine("name.StartsWith(\"Mrs\")=" + name.StartsWith("Mrs"));
            Console.WriteLine();

            Console.WriteLine("name.Substring(3,6)=" + name.Substring(3, 7));
            Console.WriteLine();

            Console.WriteLine("name.ToLower()=" + name.ToLower());
            Console.WriteLine();

            Console.WriteLine("name.ToUpper()=" + name.ToUpper());
            Console.WriteLine();
        }
    }
}

Result:

Name: Lisa
Company: MdmSoft
Salary: $2,800.00

name.CompareTo("LISA")=-1
name.CompareTo("Lisa")=0
name.CompareTo("lisa")=1

name.Contains("li")=False
name.Contains("Li")=True

name.Count()=4
company.Count()=7

name.EndsWith("sa")=True
company.EndsWith("soft")=False
company.EndsWith("soft", true, null)=True

name.Insert(0, "Mrs ")= Mrs Lisa

name.Length=8

name.PadLeft(15,'*')=.......Mrs Lisa

name.PadRight(15,'*')=Mrs Lisa.......

name.Remove(0,4)=Lisa

name.Replace("Lisa", "Sophie")=Mrs Sophie

name.StartsWith("Mrs")=True

name.Substring(3,6)= Sophie

name.ToLower()=mrs sophie

name.ToUpper()=MRS SOPHIE

Posted in C# by MdmSoft

If our work has been of help, you can help us with a small donation...
Our programmers will thank you!



All information contained in this web site are the property of MdmSoft. The information is provided "as is", MdmSoft will not be liable for any misuse of the code contained in these pages, nor can it be for inaccuracies, grammatical errors or other factors that may have caused damage or lost earnings. MdmSoft is not responsible for the content of comments posted by users.
The examples in this area have the educational and demonstration purposes only, and may be copied only for your reference, but cannot be used for commercial purposes, or for any other purpose, without the express written consent of MdmSoft.
MdmSoft also reserves the right to change, without notice, to your liking this web site, the pages and its sections, and may suspend temporarily or definitely the various services included on this site.
While using this site, you agree to have read and accepted our Terms of Service and Privacy Policy.