X Conceptual Notes String Handling
X Conceptual Notes String Handling
Instructions :
Mention date, chapter number, chapter name before starting the work.
Submission method will be - all students will click pics of solved worksheet and share pics/ pdf with
me on Google classroom
string.length()
3. The Java String charAt() method returns the character at the specified index.
string.charAt(int index)
Eg:
String str1 = "Learn Java";
System.out.println(str1.charAt(0)); // 'L'
4. The String indexOf() method returns the index of the first occurrence of the
specified character/substring within the string.
5. The String lastIndexOf() method returns the index of the last occurrence of
the specified character/substring within the string.
Eg: 1
String str1 = "Learn Java";
int result;
result = str1.lastIndexOf('J');
System.out.println(result); // 6
Eg 2:
String str1 = "Learn Java programming";
int result;
result = str1.lastIndexOf('r', 4);
System.out.println(result); // 3
6. The Java String substring() method extracts a substring from the string and
returns it.
7. The Java String replace() method replaces each matching occurrences of the
old character/text in the string with the new character/text.
or
11. The Java String toLowerCase() method converts all characters in the string to
lower case characters.
string.toLowerCase()
returns a string with all upper case letters converted to lower case letters
12. The Java String toUpperCase() method converts all characters in the string to
upper case characters.
string.toUpperCase()
13. The Java String compareTo() method compares two strings lexicographically
(in the dictionary order). The comparison is based on the Unicode value of each
character in the strings.
string.compareTo(String str)
Eg 2:
string.compareToIgnoreCase(String str)
15. The Java String trim() method returns a string with any leading
(starting) and trailing (ending) whitespace removed.
string.trim()