204111 Computer and Programming Lecture # 09: Strings and Characters Name Spaces, Enum, Struct Try-Catch Block http: / / mike.cpe.ku.ac.th/ 204111 TM approved MI KE
Class String � A string is essentially an encapsulation of an array of characters. � since we use string so often, C# defines the string class and provides many methods (behaviors). � We have already seen several methods: � method ToUpper � replaces lower case letter � original string remains unchanged � method ToLower � replaces lower case letter � original string remains unchanged TM approved MI KE 2 Version 2005/2
Class String: Creating New Strings � Constructors � C# provides eight constructors for initializing strings. � CopyTo method � copies specified number of characters into a char array. � Substring method � extracts a sub string. � Concat method � Combine two strings. TM approved MI KE 3 Version 2005/2
1 using System; 2 using System.Windows.Forms; 3 class StringConstructor { 4 static void Main( string [] args ) { 5 string output; 6 string originalString, string1, string2, string3, string4; 7 char [] characterArray = { 'b', 'i', 'r', 't', 'h', ' ', 'd', 'a', 'y' }; 8 // string initialization 9 originalString = "Welcome to C# programming!"; 10 string1 = originalString; 11 string2 = new string( characterArray ); 12 string3 = new string( characterArray, 6, 3 ); 13 string4 = new string( 'C', 5 ); 14 // combined output 15 output = "string1 = " + "\"" + string1 + "\"\n" + "string2 = " + "\"" + string2 + "\"\n" + "string3 = " + "\"" + string3 + "\"\n" + "string4 = " + "\"" + string4 + "\"\n"; 16 MessageBox.Show( output, "String Class Constructors", 17 MessageBoxButtons.OK, MessageBoxIcon.Information ); 18 } 19 } TM approved MI KE 4 Version 2005/2
StringConstructor.cs TM approved MI KE 5 Version 2005/2
1 using System; 2 using System.Windows.Forms; 3 class StringMethods { 4 static void Main(string [] args ) { 5 string string1, output; 6 char [] characterArray; 7 string1 = "hello there"; 8 characterArray = new char [5]; 9 output = "string1: \"" + string1 + "\""; 10 output += "\nLength of string1: " + string1.Length; 11 output += "\nThe string reversed is: "; 12 for ( int i = string1.Length - 1; i >= 0; i-- ) 13 output += string1[i]; 14 string1.CopyTo(0, characterArray, 0, 5); 15 output += "\nThe character array is: "; 16 for ( int i = 0 ; i < characterArray.Length; i++ ) 17 output += characterArray[i]; 18 MessageBox.Show(output, "Demonstrating the string " + "Indexer, Length Property and CopyTo method", MessageBoxButtons.OK, MessageBoxIcon.Information); 19 } // end method Main 20 } / / end class StringMethods TM approved MI KE 6 Version 2005/2
StringMethods.cs TM approved MI KE 7 Version 2005/2
1 using System; 2 using System.Windows.Forms; 3 class SubString { 4 static void Main(string [] args) { 5 string letters = "abcdefghijklmabcdefghijklm"; 6 string output = ""; 7 output += "Substring from index 20 to end is \"" + letters.Substring(20) + "\"\n"; 8 output += "Substring from index 0 to 6 is \"" + letters.Substring(0, 6) + "\""; 9 MessageBox.Show(output, "Demonstrating String method Substring", MessageBoxButtons.OK, MessageBoxIcon.Information); 10 } // end method Main 11 } // end class SubString TM approved MI KE 8 Version 2005/2
SubString.cs TM approved MI KE 9 Version 2005/2
1 using System; 2 using System.Windows.Forms; 3 class StringConcatenation { 4 static void Main(string [] args) { 5 string string1 = "Happy "; 6 string string2 = "Birthday"; 7 string output; 8 output = "string1 = \"" + string1 + "\"\n" + "string2 = \"" + string2 + "\""; 9 output += "\n\nResult of String.Concat(string1, string2) = " + String.Concat(string1, string2); 10 output += "\nstring1 after concatenation = " + string1; 11 MessageBox.Show(output, "Demonstrating String method Concat", MessageBoxButtons.OK, MessageBoxIcon.Information); 12 } // end method Main 13 } // end class StringConcatenation TM approved MI KE 10 Version 2005/2
SubConcatination.cs TM approved MI KE 11 Version 2005/2
Class String: Comparison and string Search � Comparison � Methods CompareTo and Equals � uses lexicographical comparison. � Methods StartsWith and EndsWith � Find the position of a char or string � IndexOf � IndexOfAny � LastIndexOf � LastIndexOfAny TM approved MI KE 12 Version 2005/2
1 using System; 2 using System.Windows.Forms; 3 class StringCompare { 4 static void Main(string [] args) { 5 string string1 = "hello"; string string2 = "good bye"; 6 string string3 = "Happy Birthday"; string string4 = "happy birthday"; string output; 7 output = "string1 = \"" + string1 + "\"" + "\nstring2 = \"" + string2 + "\"" + "\nstring3 = \"" + string3 + "\"" + "\nstring4 = \"" + string4 + "\"\n\n"; 8 if (string1.Equals("hello")) output += "string1 equals \"hello\"\n"; 9 else output += "string1 does not equal \"hello\"\n"; 10 if (string1 == "hello") output += "string1 equals \"hello\"\n"; 11 else output += "string1 does not equal \"hello\"\n"; 12 if (String.Equals(string3, string4)) output += "string3 equals string4\n"; 13 else output += "string3 does not equal string4\n"; 14 output += "\nstring1.CompareTo(string2) is " + string1.CompareTo(string2) + "\n" + "string2.CompareTo(string1) is " + string2.CompareTo(string1) + "\n" + "string1.CompareTo(string1) is " + string1.CompareTo(string1) + "\n" + "string3.CompareTo(string4) is " + string3.CompareTo(string4) + "\n" + "string4.CompareTo( string3 ) is " + string4.CompareTo(string3) + "\n\n"; 15 MessageBox.Show(output, "Demonstrating string " + "comparisons", MessageBoxButtons.OK, MessageBoxIcon.Information); 16 } // end method Main 17 } // end class StringCompare TM approved MI KE 13 Version 2005/2
StringCompare.cs TM approved MI KE 14 Version 2005/2
1 using System; 2 using System.Windows.Forms; 3 class StringStartEnd { 4 static void Main(string [] args) { 5 string [] strings = {"started", "starting", "ended", "ending"}; 6 string output = ""; 7 for (int i = 0; i < strings.Length; i++) 8 if (strings[i].StartsWith("st")) 9 output += "\"" + strings[i] + "\"" + " starts with \"st\"\n"; 10 output += "\n"; 11 for (int i = 0; i < strings.Length; i ++) 12 if (strings[i].EndsWith("ed")) 13 output += "\"" + strings[i] + "\"" + " ends with \"ed\"\n"; 14 MessageBox.Show(output, "Demonstrating StartsWtih and " + "EndsWith methods", MessageBoxButtons.OK, MessageBoxIcon.Information); 15 } // end method Main 16 } // end class StringStartEnd TM approved MI KE 15 Version 2005/2
StringStartEnd.cs TM approved MI KE 16 Version 2005/2
1 using System; 2 using System.Windows.Forms; 3 class StringIndexMethods { 4 static void Main(string [] args) { 5 string letters = "abcdefghijklmabcdefghijklm"; string output = ""; 6 char [] searchLetters = {'c', 'a', '$'}; 7 output += "'c' is located at index " + letters.IndexOf('c'); 8 output += "\n'a' is located at index " + letters.IndexOf('a', 1); 9 output += "\n'a' is located at index " + letters.IndexOf('a', 3, 5); 10 output += "\n\nLast 'c' is located at " + "index " + letters.LastIndexOf('c'); 11 output += "\nLast 'a' is located at index " + letters.LastIndexOf('a', 25); 12 output += "\nLast '$' is located at index " + letters.LastIndexOf('$', 15, 5); 13 output += "\n\n\"def\" is located at" + " index " + letters.IndexOf("def"); 14 output += "\n\"def\" is located at index " + letters.IndexOf("def", 7); 15 output += "\n\"hello\" is located at index " + letters.IndexOf("hello", 5, 15); 16 output += "\n\nLast \"def\" is located at index " + letters.LastIndexOf("def"); 17 output += "\nLast \"def\" is located at " + letters.LastIndexOf("def", 25); TM approved MI KE 17 Version 2005/2
Recommend
More recommend