strings

12
Declaration of string 1. strings are objects. 2. string is a reference type. 1. static string Copy(string str) 2. int CompareTo(string str) 3. int IndexOf(string str) 4. int LastIndexOf(string str) 5. string ToLower( ) 6. string ToUpper( ) A string is a set of characters enclosed by double quotes. For example, "this is a test"

Upload: kotirama

Post on 19-Dec-2015

1 views

Category:

Documents


0 download

DESCRIPTION

string s for dot net

TRANSCRIPT

Page 1: Strings

Declaration of string

1. strings are objects.2. string is a reference type.

1. static string Copy(string str)

2. int CompareTo(string str)

3. int IndexOf(string str)

4. int LastIndexOf(string str)

5. string ToLower( )

6. string ToUpper( )A string is a set of characters enclosed by double quotes. For example,

  "this is a test"  

is a string.

 

using System;

class MainClass{  static void Main(string[] args)  {    string MyString = "Hello World";    string Path = @"c:\Program Files";    string Path2 = "c:\\Program Files";

Page 2: Strings

    string Name = "Joe";

  }}

String is object

 

using System;

class MainClass{    static void Main(string[] args) {        string strOriginal = "Original String";        Console.WriteLine( "Value of strOriginal before call: {0}", strOriginal );

        TryToAlterString( strOriginal );

        Console.WriteLine( "Value of strOriginal after call: {0}", strOriginal );    }

    static void TryToAlterString(string str) {        str = "Modified String";    }}

Empty string

 

using System;using System.IO;using System.Text;

public class MainClass{    public static void Main(string[] args)    {        string address = String.Empty;    }}

Use Indexer to reference chars in a string

Page 3: Strings

 

using System;

class MainClass{

  public static void Main()  {    string myString = "To be or not to be";

        for (int count = 0; count < myString.Length; count++)    {      Console.WriteLine("myString[" + count + "] = " + myString[count]);    }

  }

}

Use the Compare() method to compare strings

 

using System;

class MainClass{

  public static void Main()  {        int result;    result = String.Compare("bbc", "abc");    Console.WriteLine("String.Compare(\"bbc\", \"abc\") = " + result);    result = String.Compare("abc", "bbc");    Console.WriteLine("String.Compare(\"abc\", \"bbc\") = " + result);    result = String.Compare("bbc", "bbc");    Console.WriteLine("String.Compare(\"bbc\", \"bbc\") = " + result);    result = String.Compare("bbc", "BBC", true);    Console.WriteLine("String.Compare(\"bbc\", \"BBC\", true) = " + result);    result = String.Compare("bbc", "BBC", false);    Console.WriteLine("String.Compare(\"bbc\", \"BBC\", false) = " + result);    result = String.Compare("Hello World", 6, "Goodbye World", 8, 5);    Console.WriteLine("String.Compare(\"Hello World\", 6, " + "\"Goodbye World\", 8, 5)

  }

}

Page 4: Strings

Use the Concat() method to concatenate strings

 

using System;

class MainClass{

  public static void Main()  {    string myString4 = String.Concat("A, ", "B");    Console.WriteLine("String.Concat(\"A, \", \"B\") = "+ myString4);    string myString5 = String.Concat("A, ", "B, ", "and countrymen");    Console.WriteLine("String.Concat(\"A, \", \"B, \", " + "\"and countrymen\") = " + myString5);  }

}

Use the addition operator (+) to concatenate strings

 

using System;

class MainClass{

  public static void Main()  {    string myString6 = "To be, " + "or not to be";    Console.WriteLine("\"To be, \" + \"or not to be\" = " + myString6);  }

}

        

Use the Copy() method to copy a string

 

Page 5: Strings

using System;

class MainClass{

  public static void Main()  {        string myString4 = "string4";    Console.WriteLine("myString4 = " + myString4);    Console.WriteLine("Copying myString4 to myString7 using Copy()");    string myString7 = String.Copy(myString4);    Console.WriteLine("myString7 = " + myString7);  }

}

Use the Equals() method and equality operator to check if two strings are equal

 

using System;

class MainClass{

  public static void Main()  {    bool boolResult;    string myString = "str";    string myString2 = "str2";        boolResult = String.Equals("bbc", "bbc");    Console.WriteLine("String.Equals(\"bbc\", \"bbc\") is " + boolResult);        boolResult = myString.Equals(myString2);    Console.WriteLine("myString.Equals(myString2) is " + boolResult);        boolResult = myString == myString2;    Console.WriteLine("myString == myString2 is " + boolResult);  }

}

Use the Format() method to format a string

Page 6: Strings

 

using System;

class MainClass{

  public static void Main()  {        float myFloat = 1234.56789f;    string myString8 = String.Format("{0, 10:f3}", myFloat);    Console.WriteLine("String.Format(\"{0, 10:f3}\", myFloat) = " + myString8);  }

}

Use the Format() method to format a string

 

using System;

class MainClass{

  public static void Main()  {        float myFloat = 1234.56789f;    string myString8 = String.Format("{0, 10:f3}", myFloat);    Console.WriteLine("String.Format(\"{0, 10:f3}\", myFloat) = " + myString8);  }

}

Use the Join() method to join strings

 

using System;

class MainClass{

Page 7: Strings

  public static void Main()  {        string[] myStrings = {"To", "be", "or", "not", "to", "be"};    string myString9 = String.Join(".", myStrings);    Console.WriteLine("myString9 = " + myString9);  }

}

Use the Split() method to split strings

 

using System;

class MainClass{

  public static void Main()  {    string[] myStrings = {"To", "be", "or", "not", "to", "be"};    string myString9 = String.Join(".", myStrings);        myStrings = myString9.Split('.');    foreach (string mySplitString in myStrings)    {      Console.WriteLine("mySplitString = " + mySplitString);    }  }

}

Use the IndexOfAny() and LastIndexOfAny() methods to search for character arrays in a string

 

using System;

class MainClass{

  public static void Main()  {

Page 8: Strings

    string[] myStrings = {"To", "be", "or", "not", "to", "be"};    string myString = String.Join(".", myStrings);           char[] myChars = {'b', 'e'};    int index = myString.IndexOfAny(myChars);    Console.WriteLine("'b' and 'e' occur at index " + index + " of myString");    index = myString.LastIndexOfAny(myChars);    Console.WriteLine("'b' and 'e' last occur at index " + index + " of myString");

  }}

Use the Insert(), Remove(), and Replace() methods to modify strings

 

using System;

class MainClass{

  public static void Main()  {    string[] myStrings = {"To", "be", "or", "not", "to", "be"};    string myString = String.Join(".", myStrings);           string myString10 = myString.Insert(6, "A, ");    Console.WriteLine("myString.Insert(6, \"A, \") = " + myString10);    string myString11 = myString10.Remove(14, 7);    Console.WriteLine("myString10.Remove(14, 7) = " + myString11);    string myString12 = myString11.Replace(',', '?');    Console.WriteLine("myString11.Replace(',', '?') = " + myString12);    string myString13 = myString12.Replace("to be", "Or not to be A");    Console.WriteLine("myString12.Replace(\"to be\", \"Or not to be A\") = " + myString13);  }

}

Use the PadLeft() and PadRight() methods to align strings

 

using System;

Page 9: Strings

class MainClass{

  public static void Main()  {    string[] myStrings = {"To", "be", "or", "not", "to", "be"};    string myString = String.Join(".", myStrings);           string myString14 = '(' + myString.PadLeft(20) + ')';    Console.WriteLine("'(' + myString.PadLeft(20) + ')' = " + myString14);    string myString15 = '(' + myString.PadLeft(20, '.') + ')';    Console.WriteLine("'(' + myString.PadLeft(20, '.') = " + myString15);    string myString16 = '(' + myString.PadRight(20) + ')';    Console.WriteLine("'(' + myString.PadRight(20) + ')' = " + myString16);    string myString17 = '(' + myString.PadRight(20, '.') + ')';    Console.WriteLine("'(' + myString.PadRight(20, '.') + ')' = " + myString17);

  }

}

Use the Trim(), TrimStart(), and TrimEnd() methods to trim strings

 

using System;

class MainClass{

  public static void Main()  {        string myString18 = '(' + "  Whitespace  ".Trim() + ')';    Console.WriteLine("'(' + \"  Whitespace  \".Trim() + ')' = " + myString18);    string myString19 = '(' + "  Whitespace  ".TrimStart() + ')';    Console.WriteLine("'(' + \"  Whitespace  \".TrimStart() + ')' = " + myString19);    string myString20 = '(' + "  Whitespace  ".TrimEnd() + ')';    Console.WriteLine("'(' + \"  Whitespace  \".TrimEnd() + ')' = " + myString20);

  }

}

Use the Substring() method to retrieve substrings

Page 10: Strings

 

using System;

class MainClass{

  public static void Main()  {        string[] myStrings = {"To", "be", "or", "not", "to", "be"};    string myString = String.Join(".", myStrings);               string myString21 = myString.Substring(3);    Console.WriteLine("myString.Substring(3) = " + myString21);    string myString22 = myString.Substring(3, 2);    Console.WriteLine("myString.Substring(3, 2) = " + myString22);

  }

}