Page 247 - Computer science 868 Class 12
P. 247
16 douvar = do1;
17 //Printing
18 System.out.println("The value of intvar : " + intvar);
19 System.out.println("The value of douvar: " + douvar);
20 }
21 }
The output of the preceding program is as follows:
The value of in : 768
The value of do: 23.45
The value of intvar : 768
The value of douvar: 23.45
8.10.2 Methods of String Class
These methods work on a group of letters enclosed within double quotations(“ ”). There are many in-built String
methods that are defined in the String class. These methods are used to perform different types of operations on the
String object.
1. int length(): This method is used to return the number of characters present in the string. It returns an integer type
of value. Length() is the only function where the counting starts from 1.
String st = "We study in class 12";
int len = st.length();
System.out.println("The length is :" +len);
Output:
The length is: 20 // All characters within " " including space
2. char charAt(int index): This method returns the character at the given index. It returns “char” type data.
String st = "We study in class 12";
char ch = st.charAt(4);
System.out.println("The character is :" +ch);
Output:
The character is: t
3. int indexOf(char ch): This method returns the index value of the first existence of the character ch present in the
String object. It returns an integer type of value.
String st = "We study in class 12";
int v = st.indexOf('t');
System.out.println("The index value is :" +v);
Output:
The index value is: 4
4. int indexOf(char ch, int index): This method returns the index value character ch present in the string of the
current String object starting from the index provided in the parameter. It returns an integer type of value.
String st = "We study in class 12";
int v = st.indexOf('s',16);
System.out.println("The index value is :" +v);
Output:
The index value is: 16
245
Methods 245

