Strings rA string is a sequence of characters that is treated as a single value r The string data type is used to represent strings in Java r We have been using String objects all along. For example, to display a text with message Box, we write messageBox. show(Hello, how are you?) C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8-6
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 6 Strings A string is a sequence of characters that is treated as a single value. The String data type is used to represent strings in Java. We have been using String objects all along. For example, to display a text with messageBox, we write messageBox.show( “Hello, how are you?” );
String is an object r String is a class in the java. lang package. Because String is a class we need to create an instance of String in Java for string processing like any other objects, we need a declaration and object creation for the instances of the String class. For example, String namely name1= new string(latte")i But we normally use a shorthand instead treating These two statements String objects much like primitive data. For example, are equivalent String ameli name1= latte C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 8-7
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 7 String is an Object String is a class in the java.lang package. Because String is a class, we need to create an instance of String in Java for string processing. Like any other objects, we need a declaration and object creation for the instances of the String class. For example, String name1; name1 = new String( “Latte” ); But we normally use a shorthand, instead, treating String objects much like primitive data. For example, String name1; name1 = “Latte”; These two statements are equivalent
Accessing Individual Elements r Individual characters in a String accessed with the charAt method String name Sumatra i 0 2 345 6 a a name name charAt(3 This variable refers to the The method returns the whole string character at position #3 C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 8-8
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 8 Accessing Individual Elements Individual characters in a String accessed with the charAt method. 0 1 2 3 4 5 6 S u m a t r a String name = “Sumatra”; name This variable refers to the whole string. name.charAt( 3 ) The method returns the character at position # 3
Determining the Size r We determine the number of characters in a String the length method String name =Sumatra str1 one/ str2= str3 namelength()i strl length()i 3 Error because no str2 length()i 0 object is created for str3. so it is a null str3 length()i Error C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8-9
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 9 Determining the Size We determine the number of characters in a String with the length method. String name = “Sumatra”, str1 = “one”, str2 = “”, str3; Error because no object is created for str3, so it is a null. name.length( ); str1.length( ); str2.length( ); str3.length( ); 7 3 0 Error!
Example: Counting Vowels char letter string name inputBox getstring ("What is your name? nt numberofCharacters name. lengthoi int yowelCount for (int 1=0; i< numberofCharacters; i++)( Here' s the code to count the number of letter name charAt(i) vowels in the input string if letter a letter letter = 'elI letter EI letter =s 'i II letter letter == 'o lett letter = uIl letter U vowelCount++ messageBox. show(name " your name has " t vowelCount vowels" C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8-10
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 8 - 10 Example: Counting Vowels char letter; String name = inputBox.getString("What is your name?"); int numberOfCharacters = name.length(); int vowelCount = 0; for (int i = 0; i < numberOfCharacters; i++) { letter = name.charAt(i); if ( letter == 'a' || letter == 'A' || letter == 'e' || letter == 'E' || letter == 'i' || letter == 'I' || letter == 'o' || letter == 'O' || letter == 'u' || letter == 'U' ) { vowelCount++; } } messageBox.show(name + ", your name has " + vowelCount + " vowels"); Here’s the code to count the number of vowels in the input string