130 likes | 290 Views
String Manipulation. Tonga Institute of Higher Education. Introduction. We know how to combine strings together and why this is useful Why is it useful to break strings apart? Extraction of data Example: My birthday is “07-07-1976”. How can we determine the year from this string?
E N D
String Manipulation Tonga Institute of Higher Education
Introduction • We know how to combine strings together and why this is useful • Why is it useful to break strings apart? • Extraction of data • Example: My birthday is “07-07-1976”. How can we determine the year from this string? • How do we break strings apart?
H e l l o 0 2 1 3 4 Index String.SubString Method 1 • Use the SubString method to get a smaller string from a larger string • Example: To get “Sione” from “Sione Tupou” • There are 2 ways to do this • SubString(StartIndex as Integer) • SubString(StartIndex as Integer, Length as Integer) • The index is the location of a letter
H e l l o 0 2 1 3 4 Index String.SubString Method 2 String.SubString(StartIndex as Integer) • The first method has only 1 parameter • It returns everything from that index to the end of the string Prints “Tupou”
H e l l o 0 2 1 3 4 Index String.SubString Method 3 SubString(StartIndex as Integer, Length as Integer) • The second method has only 2 parameter • It returns the letters starting from the first index and continuing for the specified length Prints “Tup”
H e l l o 0 2 1 3 4 Index String.IndexOf Method 1 • Use the indexOf method to get the index number of a letter or string • Example: To find that the index of “e” is 1 • -1 is returned if the string doesn’t exist • There are many different ways to use this method • String.IndexOf(value as Char) • String.IndexOf(value as Char, StartIndex as Integer) • String.IndexOf(value as Char, StartIndex as Integer, Count as Integer) • String.IndexOf(value as String) • String.IndexOf(value as String, StartIndex as Integer) • String.IndexOf(value as String, StartIndex as Integer, Count as Integer) Number of characters to search through Index to begin search from String to Find
H e l l o 0 2 1 3 4 Index String.IndexOf Method 2 • There are many different ways to use this method • String.IndexOf(value as Char) • String.IndexOf(value as Char, StartIndex as Integer) • String.IndexOf(value as Char, StartIndex as Integer, Count as Integer) • String.IndexOf(value as String) • String.IndexOf(value as String, StartIndex as Integer) • String.IndexOf(value as String, StartIndex as Integer, Count as Integer) Returns 1 Find index of string Returns -1 Returns 1
H e l l o 0 2 1 3 4 Index String.IndexOf Method 2 • There are many different ways to use this method • String.IndexOf(value as Char) • String.IndexOf(value as Char, StartIndex as Integer) • String.IndexOf(value as Char, StartIndex as Integer, Count as Integer) • String.IndexOf(value as String) • String.IndexOf(value as String, StartIndex as Integer) • String.IndexOf(value as String, StartIndex as Integer, Count as Integer) Search for “T” from 2 Returns 7 Find index of string Returns 0 Search for “T” from 0 to 5
String.Length Method • To get the number of characters in a string, use the length method Returns 12
More Methods Available • Trim – Removes whitespace before and after string • ToUpper – Changes all characters to upper case • ToLower - Changes all characters to lower case • Check the www.msdn.com or use VS.Net to get a complete list of the methods available