Chapter 9: String, Stringbuffer, Stringbuilder (3 HRS) Chapter Objectives
Chapter 9: String, Stringbuffer, Stringbuilder (3 HRS) Chapter Objectives
s List the purpose and difference of String, StringBuffer, StringBuilder classes The String Class The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created. StringBuffers and StringBuilders support mutable strings. Because String objects are immutable they can be shared. For example: String str = "abc"; is equivalent to: char data[] = {'a', 'b', 'c'}; String str = new String(data);
Here are some more examples of how strings can be used: System.out.println("abc"); String cde = "cde"; System.out.println("abc" + cde); String c = "abc".substring(2,3); String d = cde.substring(1, 2);
What are the uses of String Data Type? - The string data type is used for string of characters such as names, addresses, phone numbers and so on. Example: Lawrence Java Programming 1234567890 String Position vs. Length Example: 1. L A W R E N C E 2. 0 1 2 3 4 5 6 7 Position 3. 1 2 3 4 5 6 7 8 Length Blanks and punctuation marks are characters
Page 1 of 8
Example: 1. System.out.println(Congratulations! You passed this subject.); 2. The String inside System.out.println( ) is a string constant. 3. In an output statement it is possible to concatenate string constants and numbers using the + operator. The number is automatically converted to a string. Example: int grade = 70; System.out.println(Your grade is + grade); The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase to lowercase. Declaring String Objects String str = abc; is equivalent to: char data[] = {a, b, c}; String str = new String(data); and String str; str = new String(abc); Some String Methods
substring() length() charAt() concat() replace() toLowerCase() toUpperCase() equals() equalsIgnoreCase() compareTo() trim() split()
Example 1: String country = Philippines; System.out.println(country.substring(3)); Output: lippines Starting at character position number 3 to the end of the string Example 2: String country = Philippines; System.out.println(country.substring(0,3)); Output: Phi characters in position numbered 0 to the number of length. Example 3: String country = Philippines System.out.println(country.substring(9, 12); Output: Error Produces an exception since the word has character position only from 0 to 10. The length() method Provide the number of characters in the string. Example 1: String country = Philippines; System.out.println(country.length()); Output: 11 To find the last character of the string we can use this statement: Example 2: String country = Philippines; System.out.println(country.substring(country.length()-1)); Output: s The charAt() method Is used to fine the character at any string position. Example 1: String country = Philippines; System.out.println(country.charAt(1)); Output: h
Page 3 of 8
Example 2: String country = Philippines; System.out.println(Philippines.charAt(1)); Output: h Concatenation of Strings The concatenation operator + can be used in string expressions. Example 1: String a = abc; String b = def; String c = a + c; System.out.println(c); System.out.println(c + ghi); Output: abcdef abcdefghi The concat() method It is used to concatenate strings. Example 1: String a = abc; String b = def; String c = a.concat(b); System.out.println(c); System.out.println(c.concat( ghi)); Output: abcdef abcdefghi int age = 21; System.out.println(age); Output: 21 String age = 21; System.out.println(age); Output: 21 String age = 21;
Page 4 of 8
System.out.println(age); Output: Data type mismatch System.out.println(2 + 3 + is five); Output: 5 is five System.out.println(2+3+ is five is Output: 5 is five is 23 +2+3);
System.out.println(2+3+ is five is +(2+3)+2+ 3); Output: 5 is five is 523 System.out.println(The average is +80+87/2); Output: The average is 8043 System.out.println(The average is + 80 + (double) 87/2); Output: The average is 8043.5000000 System.out.println(The ); System.out.println(quick); Output: The quick System.out.print(The ); System.out.print(quick); Output: The quick The replace() method Replacing characters in String Example: String a = Juan String b = a.replace(u, e); System.out.println(a); System.out.println(b); Output: Juan Jean The toLowerCase() and toUpperCase() methods Converting string from uppercase to lowercase and vice versa. String a = Juan; System.out.println(a.toUpperCase());
Page 5 of 8 Prepared by: Lawrence G. Decamora III, scjp
Output: JUAN String a = Juan; System.out.println(a.toLowerCase()); Output: juan The equals() method String a = Juan; String b = Peter; System.out.println(a.equals(b)); Output: false a.equals(b) is true if a and b are strings of the same length and they have the same characters in the same sequence. String a = Peter; String b = Peter; System.out.println(a.equals(b)); Output: true The equalsIgnoreCase() method String a = Peter; String b = peter; System.out.println(a.equals(b)); Output: false String a = Peter; String b = peter; System.out.println(a.equalsIgnoreCase(b)); Output: true The compareTo() method It is similar with equals String a = Peter; String b = Peter; System.out.println(a.compareTo(b)); Output: 0 if a and b are equal the value is 0 if a is less than b the value is negative if a comes after b the value is positive The trim() method Is used to remove white spaces
Page 6 of 8 Prepared by: Lawrence G. Decamora III, scjp
String a = Peter ; System.out.println(a.trim( )); System.out.println(a); Output: Peter Peter The split() method Splits this string around matches of the given regular expressions. String str = aaa:bbb:ccc; String results[] = str.split(:); for (String r : results) { System.out.println(r); } Output: aaa bbb ccc The StringBuffer and StringBuilder classes StringBuffer A thread-safe, mutable sequence of characters. A StringBuffer is like a String, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. StringBuffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved. The principal operations on a StringBuffer are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append method always adds these characters at the end of the buffer; the insert method adds the characters at a specified point. StringBuilder A mutable sequence of characters. This class provides an API compatible with StringBuffer, but with no guarantee of synchronization. This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations. The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string
Page 7 of 8 Prepared by: Lawrence G. Decamora III, scjp
and then appends or inserts the characters of that string to the string builder. The append method always adds these characters at the end of the builder; the insert method adds the characters at a specified point. The append() and the insert() method on StringBuffer and StringBuilder classes Example1: StringBuffer sb = new StringBuffer("abc"); sb.append("def"); System.out.println(sb); Output: Example2: StringBuffer sb = new StringBuffer("abc"); sb.insert(1, "def"); System.out.println(sb); Output: adefbc These methods will have the same effect on both StringBuffer and StringBuilder classes abcdef
Page 8 of 8