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