1 / 32

String Manipulation

String Manipulation. Students should fill out String Worksheet along with this. Students can use worksheet on test. Overview. String manipulation, like most programming tools, are easy to understand but difficult to know how to use.

ranae
Télécharger la présentation

String Manipulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String Manipulation Students should fill out String Worksheet along with this. Students can use worksheet on test.

  2. Overview • String manipulation, like most programming tools, are easy to understand but difficult to know how to use. • Most string functions are never used just by themselves. Typically they are used in combination with other string functions, loops, if statements etc… • The following slides are only a few of the string functions available.

  3. Overview • Strings are really just a bunch of characters all put together. It is important to understand that the first character is the ZERO character. • When counting characters it would like:

  4. Toupper and tolower

  5. Length • Simply returns how many characters exist in a string. • Syntax: string.length • Examples: Dim x As String = "Green H" MessageBox.Show(x.Length) ‘Displays 7 MessageBox.Show("Help".Length) ‘Displays 4 Dim x As String = "Green H" For i = 0 To x.Length‘run a loop dependent on length statement(s) Next i

  6. Trim • Removes all leading and trailing white-space characters from a string. (For us white-space will mean spaces, but it includes tab, linefeed, carriage-return, formfeed, vertical-tab, and newline characters ) • Syntax: string.Trim[(char,…,char)] • It only does it at the start and end of the string. • Examples Dim x As String = " Green H " MessageBox.Show(x.Length) ‘Displays 13 x = x.Trim MessageBox.Show(x.Length) ‘Displays 7

  7. Trim continued • You can specify what characters you want to remove. Dim x As String = " TEricaE% " x = x.Trim("E"c, "%"c) ‘remove leading &trailing E and % MessageBox.Show(x) ‘Displays “ TErica” spaces are left Dim x As String = " TEricaE%B " x = x.Trim("E"c, "%"c) ‘remove leading &trailing E and % MessageBox.Show(x) ‘Displays “ TEricaE%B ” spaces are left

  8. TrimStart • The exact same as TRIM but it only works at the start of the string.

  9. TrimEnd • The exact same as TRIM but it only works at the end of the string.

  10. Remove • Used to remove any character from anywhere in a string. • Syntax: string.Remove(startIndex, count) • Examples: Dim x As String = "Hey Herotp" x = x.Remove(8, 2) ‘x is now “Hey Hero” x = "Hey Herotp" x = x.Remove(5) ‘x is now Hey H

  11. Replace • Replaces all occurrences of a sequence of characters in a string with another sequence of characters. • Syntax: string.replace(oldvalue,newvalue) • Examples: Dim x As String = "The dog do Do" x = x.Replace("d", "H") ‘x = “The Hog Ho Do” Dim x As String = "The dog do do" x = x.Replace("do", "ccc") ‘x = “The cccg ccc ccc”

  12. Mid Statement (Used to replace part of string) • Replaces a specific number of characters in a string with characters from another string. • Syntax:Mid(targetString, start [,count]) = replacementString

  13. Mid Statement continued • Examples: Dim TestString As String = "The dog jumps" Mid(TestString, 5, 3) = "fox" 'Returns "The fox jumps". Mid(TestString, 5) = "cow" 'Returns "The cow jumps". Mid(TestString, 5) = "cow jumped over" 'Returns "The cow jumpe". Mid(TestString, 5, 3) = "duck" 'Returns "The ducjumpe". Mid(TestString, 5, 1) = “game" 'Returns "The gucjumpe".

  14. padleft • Adds characters to the start of a string until a string is a specified length. • This is used with database work and fixed record sizes. • Syntax: string.padleft(length[, character])If character parameter is missing a space is default • Examples: Dim x As String = "687 6985" x = x.PadLeft(10, "*"c) ‘x = “**687 6985”

  15. padright • Adds characters to the end of a string until a string is a specified length. • Syntax: string.padright(length[, character])If character parameter is missing a space is default • Examples: Dim x As String = "687 6985" x = x.PadRight(10, "*"c) ‘x = “687 6985**”

  16. insert • Inserts characters within a string. (One based) • Syntax: string.insert(startindex,value) • Examples: Dim ssn As String = 987654321 ssn = ssn.Insert(3, "-") ‘You get 987-654321 ssn = ssn.Insert(6, "-") ‘You get 987-65-4321

  17. Searching Strings • The next four methods deal specifically with searching strings. • These search methods are mainly used for conditional checks in IF and LOOP statements, or they return a numerical value stating what place a character is at in a string.

  18. startswith • Determines whether a specific sequence of characters occurs at the beginning of a string. Returns a Boolean value. It is case sensitive. • Syntax: string.startswith(substring) • Examples: Dim x As String = "Green High School" If x.StartsWith("Green") Then MessageBox.Show("We found Green") End If

  19. endswith • Determines whether a specific sequence of characters occurs at the end of a string. Returns a Boolean value. It is case sensitive. • Syntax: string.endswith(substring) • Examples: Dim x As String = "Green High School" If x.endsWith("Green") Then MessageBox.Show("Ends with Green") Else MessageBox.Show("Does not end with Green") End If

  20. contains • Used to determine whether a substring exists anywhere within a string. Returns a Boolean. It is case sensitive. • Syntax: string.contains(substring) • Examples: Dim x As String = "I go to Green High School" If x.Contains("Green") Then MessageBox.Show("Green found") End If

  21. indexof • Searches a string for a substring and if it is found it returns an integer value that represents the location of the substring being searched for. If it is not found then a -1 value is returned. (Zero Based) • Syntax: string.IndexOf(substring[, startIndex]) • Examples: Dim x As String = "I go to Green High School" Dim y As Int16 y = x.IndexOf("Green") ’Returns 8 y = x.IndexOf("Green",9) ’Returns -1 y = x.IndexOf("o") ’Returns 3 y = x.IndexOf("High", 9) ‘Returns 14

  22. Substring • This method will let you pull out a substring contained in a string. • Syntax: string.SubString(startIndex[,count]) • Examples: Dim fullname As String = "Barack Obama" Dim firstname, lastname As String firstname= fullname.Substring(0, 6) 'Returns Barack lastname= fullname.Substring(6) 'Returns Obama MessageBox.Show(fullname.Substring(2, 4)) 'Displays rack

  23. String.compare • Simply compares two strings and returns an Integer. The optional ignoreCase parameter is by default false. • Three values are returned • 0 – means the strings are equal • 1 – means string 1 is > string 2 (string 2 would come first alphabetically) • -1 – means string 1 is < string 2 (string 1 would come first alphabetically) • This method uses Word Sort Rules which states that numbers are less than lowercase letters and lowercase is less than uppercase. • Syntax: string.compare(string1, string2[,ignoreCase]) • Examples:

  24. String.Compare Examples String.Compare("ERIC", "eric") 'returns 1 String.Compare("eric", "ERIC") 'returns -1 String.Compare("ERIC", "ERIC") 'returns 0 String.Compare("eric", "ERIC", True) 'returns 0 String.Compare(“Bob", “Eric") 'returns -1 String.Compare(“E", “e") 'returns 1 String.Compare(“3", “e") 'returns -1

  25. like • Compares two strings but the comparison is based on patterns and you create the patterns. Examples: • You want every string that starts with the word “New” • You want every string with 3 numbers at front of string. • You want every string that contains a a range of characters. • A Boolean value is returned. • Syntax: string like pattern • Patterns can involve cryptic things like *, ?, #, [list]. The following slides explain the pattern possibilities. This is a powerful tool.

  26. Like Examples using * pattern The * is for zero or more characters. If state Like "M*" Then MessageBox.Show("The states first letter is M") End If If state Like “*o" Then MessageBox.Show("The states last letter is o") End If If state Like “*nn*" Then MessageBox.Show("The state contains the letter s nn somewhere in the middle of name”) End if

  27. Like Examples using ? pattern The ? is for any single character. "BAT123khg" Like "B?T“ This returns false. “BT" Like "B?T“ This returns false. “byT" Like "B?T“ This returns false. “ByT" Like "B?T“ This returns true. “B8T" Like "B?T“ This returns true.

  28. Like Examples using # pattern • Used to compare against any single digit 0-9 "hi5" Like "hi#" Returns true. "hi5" Like "hithere*" Returns false. "4allh8ters" Like "#allh#ters" Returns true. "(330) 699-8765" Like "(###) ###-####" Returns true. "(33) 699-8765" Like "(###) ###-####" Returns false.

  29. Like Examples with ?, #, * • You tell me true or false False "CABT123khg" Like "C*T#khg" True "CABT3khg" Like "C*T#k*" "CAT3khg" Like "C??T3*g" False "CAT3khg" Like "C?T3*g" True

  30. Like Examples using [charList] For i = 0 To wordstr.Length - 1 If wordstr.Substring(i, 1) Like "[0-9]" Then MessageBox.Show("No numbers are allowed") End If Next wordstr.Substring(i, 1) Like "[!0-9]“ wordstr.Substring(i, 1) Like "[a-zA-Z]“ wordstr.Substring(i, 1) Like “B[AEIOU]T"

  31. Create a program that allows a user to enter a password that contains 5,6 or 7 characters. The application will display a new password using three rules. • Replace all vowels with the letter X. • Replace all numbers with the letter Z. • Reverse the characters in the password.

  32. Phone Number Problem • Everyone types in phone numbers differently. Examples are 330 655 1234 or (330) 655-1234 or 330.699.1234 or 3306551234 or 330-655-1234 etc... However people type a number is up to them but as a computer this cause problems. A computer only likes phone numbers in one method. • You are to write code that will accept a phone number to be typed in in any method but will format it to look like 330.456.1234 format. Once a user types in the number and clicks Okay then a properly formatted phone number will appear in the same box the bad formatted number came from. • How do you do this • Get number and extract all characters that aren't numbers. • Ensure that 10 digits are entered, otherwise have user do it again with a popup. • Put a period in after the third and sixth number.

More Related