Page 355 - Computer science 868 Class 12
P. 355
Method Example Output
append() StringBuffer s1 = new StringBuffer("comp") ; computer
StringBuffer s2 = new StringBuffer("uter");
System.out.println(s1.append(s2));
setCharAt() StringBuffer s1 = new StringBuffer("Rimi"); Rima
s1.setCharAt(3,'a');
System.out.println(s1);
insert() StringBuffer s1 = new StringBuffer("Rimi a Rimi is a good student
good student");
StringBuffer s2 = new StringBuffer("is ");
s1.insert(5,s2);
System.out.println(s1);
delete() StringBuffer s1 = new StringBuffer("indiiaan"); indian
s1.delete(4,6);
System.out.println(s1);
setLength() StringBuffer s1 = new StringBuffer("India India is a country India
is a country"); is a
System.out.println(s1); Note: This shows that
the extra characters are
s1.setLength(10);
removed.
System.out.println(s1);
reverse() StringBuffer s1 = new StringBuffer("India India is a country
is a country");
yrtnuoc a si aidnI
System.out.println(s1);
Note: The sentence is
s1.reverse(); reversed character wise
System.out.println(s1);
10.4 CONVERSION FROM STRING TO PRIMITIVE DATA TYPES AND VICE VERSA
10.4.1 Use of Wrapper class to do Conversions
1. Converts String to primitive data types:
• <Wrapper class>.valueOf(String str): This method is used to convert the string str to its primitive data type using
the wrapper class.
Syntax: <primitive type> <variable> = <Wrapper class>.valueOf(String str);
For example,
String str1= "234";
int n=Integer.valueOf(str1); // converts "234" to 234
String str1= "27.83";
double d=Double.valueOf(str1); // converts "27.83" to 27.83
• <Wrapperclass>.pasre<primitive datatype>(String): This method is also used to do the same conversions.
For example,
String str1= "234";
int n=Integer.pasreInt(str1); // converts "234" to 234
String str1= "27.83";
double d=Double.parseDouble(str1); // converts "27.83" to 27.83
353
Strings 353

