Page 269 - Cs_withBlue_J_C11_Flipbook
P. 269
Output:
I am going to play
Note: There are two spaces between “am going” as only the word “not” is removed.
The setLength() Method
We use the setLength() method to set the length of the String Buffer variable. This also truncates the extra characters
if a string of more characters is entered. The syntax of the method is:
StringBuffer string_object.setLength(Number of characters);
Let us see the below example:
StringBuffer s1= new StringBuffer("I am not going to play");
s1.setLength(10);
System.out.println(s1);
Output:
I am not g
Since 10 characters can accommodate, the stringbuffer object will contain “I am not g” and the rest will truncate.
The reverse() Method
We use the reverse() method to reverse the characters in the given string. The syntax of the method is:
StringBuffer_object.reverse();
Let us see the below example:
StringBuffer st= new StringBuffer("Computer");
st.reverse();
System.out.println(st);
Output:
retupmoC
Let us see the below example:
StringBuffer st=new StringBuffer("Madam");
st.reverse();
System.out.println(st);
Output:
madaM
10.4 STRING METHODS UNDER WRAPPER CLASS
Wrapper class provides the mechanism to use the primitive data types such as byte, short, int, long, char and Boolean
as objects.
The valueOf(String str) Method
This method is used to convert the string str to its primitive data types using the wrapper class. The syntax is:
<primitive type> <variable>= <Wrapper class>.valueOf(String str);
Let us see the below example:
String str1= "2022";
int n=Integer.valueOf(str1);
// converts "2022" to 2022.
String str2= "20.23";
267
Strings 267

