Download presentation
Presentation is loading. Please wait.
Published byMichael Walton Modified over 8 years ago
1
String Manipulation
2
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”;
3
Concatenation operator Strings can be concatenated (“added together”) using the concatenation operator + String first = “Tammy”; String last = “Bailey”; String name = first + “ ” + last; String greeting = “Hello ” + first; The string variable name contains the string “Tammy Bailey” The string variable greeting contains the string “Hello Tammy”
4
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
5
String characters char charAt(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 toLowerCase() –returns new string with all characters in lowercase Examples String name = “Tammy Bailey”; char c = name.charAt(8); String s1 = name.replace(‘y’,‘i’); String s2 = name.toLowerCase();
6
String indices int indexOf(String s) –returns the index of the first occurrence of the specified substring int indexOf(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”; int idx1 = name.indexOf(“y”); int idx2 = name.indexOf(“mm”); int idx3 = name.indexOf(“Z”); int idx4 = name.indexOf(“y”,5); idx1=4 idx2=2 idx3=-1 idx4=11
7
Substrings String substring(int startIdx) –returns a new string that is a substring of the string –the substring begins with the character at startIdx and extends to the end of the string String substring(int startIdx, int endIdx) –returns a new string that is a substring of the string –the substring begins at startIdx and extends to the character at index endIdx-1 –the length of the substring is endIdx-startIdx
8
Substring examples String name = “Tammy Bailey”; String sub1 = name.substring(8); String sub2 = name.substring(0,3); String sub3 = name.substring(name.indexOf(‘B’)); String sub4 = name.substring(0,name.indexOf(“ ”)); sub1 = “iley” sub2 = “Tam” sub3 = “Bailey” sub4 = “Tammy”
9
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.indexOf(“ ”); }
10
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.indexOf(“ ”, index+1); }
11
Parsing strings String email = “tammy@cs.duke.edu”; String login = email.substring(0,email.indexOf(“@”)); String host = email.substring(email.indexOf(“@”)+1); String name = “Bailey, Tammy”; String first = name.substring(name.indexOf(“,”)+1); String last = name.substring(0,name.indexOf(“,”));
12
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
13
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, indexOf, charAt
14
Subroutine FindFirstVowel int FindFirstVowel(String word) { char c; int index = -1; word = word.toLowerCase(); while(index < word.length()) { c = word.charAt(index); if(c==‘a’||c==‘e’||c==‘i’||c==‘o’||c==‘u’) return index; index++; } return –1; } returns index of first vowel found in word or –1 if word contains no vowels
15
Conversion cases Use the integer returned by FindFirstVowel to determine how to convert the word to Pig Latin Three cases – FindFirstVowel returns 0 word begins with vowel – FindFirstVowel returns –1 no vowels in world – FindFirstVowel returns positive integer word begins with a consonant know index of first occurrence of vowel
16
Subroutine Convert String Convert(String s) { int i = FindFirstVowel(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 */ }
17
Text Areas A TextArea is similar to a TextField –Differences: has both rows and columns, has scrollbars, can append text Declare a TextArea object TextArea tArea; Initialize tArea with 5 rows and 60 columns tArea = new TextArea(5,60); Set text in TextArea tArea.setText(“Hello”); Append text to existing text in tArea tArea.append(“ there”); – tArea now contains the text “Hello there”
18
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 TextArea 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 tArea.setText(“Hello.”); tArea.append(“\n”); tArea.append(“How are you?”);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.