String Manipulation Java String class The String class


















- Slides: 18

String Manipulation

Java String class ® The String class represents character strings “Tammy Bailey” All strings (arrays of characters) in Java programs are implemented as instances of this class ® Declaration String name; ® ® Assignment name = “Tammy Bailey”; ® Declare and initialize String name = “Tammy Bailey”;

Concatenation operator ® Strings can be concatenated (“added together”) using the concatenation operator + String first = “Tammy”; last = “Bailey”; name = first + “ ” + last; greeting = “Hello ” + first; The string variable name contains the string “Tammy Bailey” ® The string variable greeting contains the string “Hello Tammy” ®

String indexing and length Characters in strings are indexed the same as arrays – first index is 0 – last index is one less than the number of characters ® The length of a string is the number of characters in the string – letters, numbers, spaces, punctuation, etc. – the length method returns the length of a string ® ® Example String name = “Tammy Bailey”; int len = name. length(); – The value of len is 12

String characters char. At(int index) – returns the character at the specified index ® String replace(char old, char new) – returns new string resulting from replacing all occurrences of the old character with new ® String to. Lower. Case() – returns new string with all characters in lowercase ® Examples String name = “Tammy Bailey”; char c = name. char. At(8); String s 1 = name. replace(‘y’, ‘i’); String s 2 = name. to. Lower. Case(); ®

String indices int index. Of(String s) – returns the index of the first occurrence of the specified substring ® int index. Of(String s, int index) – returns the index of the first occurrence of the specified substring, starting at the specified index ® Examples String name = “Tammy Bailey”; idx 1=4 int idx 1 = name. index. Of(“y”); idx 2=2 int idx 2 = name. index. Of(“mm”); idx 3=-1 int idx 3 = name. index. Of(“Z”); idx 4=11 int idx 4 = name. index. Of(“y”, 5); ®

Substrings ® String substring(int start. Idx) – returns a new string that is a substring of the string – the substring begins with the character at start. Idx and extends to the end of the string ® String substring(int start. Idx, int end. Idx) – returns a new string that is a substring of the string – the substring begins at start. Idx and extends to the character at index end. Idx-1 – the length of the substring is end. Idx-start. Idx

Substring examples String name = “Tammy Bailey”; String sub 1 = name. substring(8); String sub 2 = name. substring(0, 3); String sub 3 = name. substring(name. index. Of(‘B’)); String sub 4 = name. substring(0, name. index. Of(“ ”)); • • sub 1 sub 2 sub 3 sub 4 = = “iley” “Tam” “Bailey” “Tammy”

Strings and loops ® What does the string name contain after exiting the while-loop? String name = “Tammy Lynn Bailey”; int index = 0; while(index != -1) { name = name. substring(index); index = name. index. Of(“ ”); }

Another example ® What is the value of count after exiting the while-loop? String s = “How many words are there? ”; int count = 0; int index = 0; while(index != -1) { count++; index = s. index. Of(“ ”, index+1); }

Parsing strings ® String email = “tammy@cs. duke. edu”; String login = email. substring(0, email. index. Of(“@”)); String host = email. substring(email. index. Of(“@”)+1); ® String name = “Bailey, Tammy”; String first = name. substring(name. index. Of(“, ”)+1); String last = name. substring(0, name. index. Of(“, ”));

Pig Latin If a word begins with a consonant, move all of the characters up to the first vowel to the end of the word and add “ay” – bad becomes adbay ® If a word begins with a vowel, write the word and add “way” – add becomes addway ® Y is not a vowel. – yes becomes esyay ® If there are no vowels in the word, just write the word – why becomes why ®

Pig Latin Converter How to design a program to convert words to Pig Latin? ® Need to find first occurrence of a vowel – word begins with vowel, or – word begins with consonant and we move all letters up to first vowel, or – there are no vowels in the word ® Idea: loop through characters in word until vowel is found or we go through all characters (no vowels) ® String methods to use – length, index. Of, char. At ®

Subroutine Find. First. Vowel int Find. First. Vowel(String word) returns index of first vowel found in word { or – 1 if word contains char c; no vowels int index = -1; word = word. to. Lower. Case(); while(index < word. length()) { c = word. char. At(index); if(c==‘a’||c==‘e’||c==‘i’||c==‘o’||c==‘u’) return index; index++; } return – 1; }

Conversion cases Use the integer returned by Find. First. Vowel to determine how to convert the word to Pig Latin ® Three cases – Find. First. Vowel returns 0 ® • word begins with vowel – Find. First. Vowel returns – 1 • no vowels in world – Find. First. Vowel returns positive integer • word begins with a consonant • know index of first occurrence of vowel

Subroutine Convert String Convert(String s) { int i = Find. First. Vowel(s); if( i == 0 ) { /* word begins with vowel */ } else if( i > 0 ) { /* word begins with consonant but has a vowel */ } else { /* word has no vowels */ } }

Text Areas ® ® ® A Text. Area is similar to a Text. Field – Differences: has both rows and columns, has scrollbars, can append text Declare a Text. Area object Text. Area; Initialize t. Area with 5 rows and 60 columns t. Area = new Text. Area(5, 60); Set text in Text. Area. set. Text(“Hello”); Append text to existing text in t. Area. append(“ there”); – t. Area now contains the text “Hello there”

Newline character The newline character ‘n’ terminates a line of text – any text following the newline character begins on the next line – used in display/formatting of output text ® Example – a Text. Area object displays multiple lines of text – can append the newline character to the end of the output string – subsequent output will display on a new line t. Area. set. Text(“Hello. ”); t. Area. append(“n”); t. Area. append(“How are you? ”); ®