Page 418 - CA_Blue( J )_Class10
P. 418
For example:
String str=new String();
str = "INDIA";
OR
String str=new String("INDIA");
The new keyword is used here to create the String object dynamically.
16.2 HOW STRING IS STORED IN MEMORY?
Internally, a string is stored in the memory similar to character array. Each character has a different index. Similar to
array, indexing starts with 0. For example, the word "Computer" is stored in memory in the following manner:
Str C o m p u t e r
Index 0 1 2 3 4 5 6 7
Thus, it is proved that in a String the characters are kept in the memory as elements are stored in an array. The index
value of the String object starts from 0 and continues till length()-1. The length() method is used to find the length of
the String.
16.3 METHODS OF THE STRING CLASS
There are many in-built methods that are defined in the String class. These methods are used to perform different
types of operations on the String object. Let us learn about them.
16.3.1 The length() Method
The length() method is used to return the number of characters present in the string. It always returns an integer value.
The length() is the only method in which the counting starts from 1. The syntax to use the length() method is:
int <variable> = <String_Object>.length();
For example:
String str = "Sub: Computer Applications, Class 10";
int len = str.length();
System.out.println("The length is: " +len);
You will get the following output:
The length is: 36
Note: All the methods of the String class are accessed using the object of the String class.
16.3.2 The charAt(int index) Method
The charAt() method returns the character in the given index. It takes index as an integer value in the parameter and
returns the char type data. The syntax to use the charAt() method is:
char <variable> = <String_Object>.charAt(int index);
For example:
String str = "Class 10 ICSE";
char ch = str.charAt(4);
System.out.println("The character is: " +ch);
You will get the following output:
The character is: s
16.3.3 The indexOf() Method
The indexOf() method returns the index of the first occurrence of the character passed as parameter in the String
object. It takes a character as parameter and returns the index as an integer value. The syntax to use the indexOf()
416416 Touchpad Computer Applications-X

