String edit and convert methods - CSharp Language Basics

CSharp examples for Language Basics:string

Description

String edit and convert methods

Demo Code

using System;//from   w  w  w.ja va  2s .  co  m
using System.Text;
class Program
{
   static void Main(string[] args)
   {
      string favoriteFood = "cheeseburgers";
      // Substring().
      Console.WriteLine("\nSubstring() method:");
      Console.WriteLine("Substring 'burgers' is: {0}", favoriteFood.Substring(6, favoriteFood.Length - 6));
      // IsNullOrEmpty().
      Console.WriteLine("\nIsNullOrEmpty() method - note how this is called:");
      Console.WriteLine("'cheeseburgers' is an empty string: {0}", string.IsNullOrEmpty(favoriteFood));
      string empty1 = string.Empty;  // One way to initialize an empty string.
      string empty2 = "";            // Another way.
      Console.WriteLine("empty1 is empty ({0}), empty2 is empty ({1})", string.IsNullOrEmpty(empty1), string.IsNullOrEmpty(empty2));
      // Join().
      Console.WriteLine("\nJoin() method - concatenate strings with " + "divider chars - note how this is called:");
      string[] brothers = { "Chuck", "Bob", "Steve", "Mike" };
      string theBrothers = string.Join(":", brothers);
      Console.WriteLine("brothers: {0}", theBrothers);
      // Copy().
      Console.WriteLine("\nCopy() method - returns a copy of the initial string:");
      string jojo = "jojo";
      string jojoCopy = string.Copy(jojo);
      Console.WriteLine("Copy of 'jojo' is {0}", jojoCopy);
      // StartsWith() and EndsWith().
      Console.WriteLine("\nStartsWith() and EndsWith() methods:");
      Console.WriteLine("'jojo' starts with 'jo': {0}", jojo.StartsWith("jo"));
      Console.WriteLine("'jojo' ends with 'jo': {0}", jojo.EndsWith("jo"));
      // GetType().
      Console.WriteLine("\nGetType() method - call this on a " +
      "type to get a string containing its name:");
      Console.WriteLine("The string 'jojo' is of type {0}", jojo.GetType());
      Console.WriteLine("The int 3 is of type {0}", 3.GetType());
      Console.WriteLine("This class is of type {0}", new Program().GetType());
      // Insert().
      Console.WriteLine("\nInsert() method:");
      // Recall that "changing" a string doesn't change it -
      // it just returns a new string with the changes.
      jojo = jojo.Insert(2, "bo");
      Console.WriteLine("Inserting 'bo' into 'jojo': {0}", jojo);
      // Remove().
      Console.WriteLine("\nRemove() method:");
      jojo = jojo.Remove(2, 2);  // Remove 2 chars starting at index 2.
      Console.WriteLine("Removing 'bo' from 'jobojo': {0}", jojo);
      // Replace().
      Console.WriteLine("\nReplace() method:");
      string cheeseburgerWithoutEs = favoriteFood.Replace('e', 'X');
      Console.WriteLine("After Replace('e', 'X'), cheeseburger becomes {0}",
      cheeseburgerWithoutEs);
      // ToCharArray().
      Console.WriteLine("\nToCharArray() method - returns a " + "char[] with the string's chars:");
      string someChars = ";:?.,";
      char[] theChars = someChars.ToCharArray();
      Console.WriteLine("Splitting ';:?.,' into chars results in {0} chars: ", theChars.Length);
      foreach (char c in theChars)
      {
         Console.WriteLine(c);
      }
   }
}

Result


Related Tutorials