Some of the commonly used String methods are:
String name = “Aryadeep”;
(Indexes of the above string are as follows: 0-A, 1-r, 2-y, 3-a, 4-d, 5-e, 6-e, 7-p )
1. length() – Returns the length of String name. (8 in this case)
2. toLowerCase() – Returns a new String which has all the lowercase characters from the String name.
3. toUpperCase() – Returns a new String which has all the uppercase characters from the String name.
4. trim() – Returns a new String after removing all the leading and trailing spaces from the original string.
5. substring(int start) – Returns a substring from start to the end. Substring(3) returns “adeep”. [Note that index starts from 0]
6. substring(int start, int end) – Returns a substring from the start index to the end index. The start index is included and the end is excluded.
7. replace(‘r’, ‘p’) – Returns a new string after replacing r with p. Apyadeep is returned in this case. (This method takes char as argument)
8. startsWith(“Ar”) – Returns true if name starts with string “Ar”. (True in this case)
9. endsWith(“ep”) – Returns true if name ends with string “ry”. (True in this case)
10. charAt(2) – Returns the character at a given index position. (y in this case)
11. indexOf(“s”) – Returns the index of the given string.
For e.g. name.indexOf(“r”) returns 1 which is the first occurrence of r in string “Aryadeep”, -1 otherwise.
12. indexOf(“s”, 3) – Returns the index of the given String starting from index 3(int). (-1 is returned in this case)
13. lastIndexOf(“e”) – Returns the last index of the given string. (3 in this case)
14. lastIndexOf(“n”,4) – Returns the last index of the given string before index 6.
15. equals(“Harry”) – Returns true if the given string is equal to “Harry” false otherwise [Case sensitive]
16. equalsIgnoreCase(“harry”) – Returns true if two strings are equal ignoring the case of characters.
Escape Sequence Characters
The sequence of characters after backslash ‘\’ = Escape Sequence Characters
Escape Sequence Characters consist of more than one character but represent one character when used within the strings.
Examples: \n (newline), \t (tab), \’ (single quote), \\ (backslash), etc.
Code as described in the video
package com.company;
public class cwh_14_string_methods {
public static void main(String[] args) {
String name = "Harry";
// System.out.println(name);
int value = name.length();
//System.out.println(value);
//String lstring = name.toLowerCase();
//System.out.println(lstring);
//String ustring = name.toUpperCase();
//System.out.println(ustring);
//String nonTrimmedString = " Harry ";
//System.out.println(nonTrimmedString);
//String trimmedString = nonTrimmedString.trim();
//System.out.println(trimmedString);
//System.out.println(name.substring(1));
//System.out.println(name.substring(1,5));
//System.out.println(name.replace('r', 'p'));
//System.out.println(name.replace("r", "ier"));
//System.out.println(name.startsWith("Har"));
//System.out.println(name.endsWith("dd"));
//System.out.println(name.charAt(4));
//String modifiedName = "Harryrryrry";
//System.out.println(modifiedName.indexOf("rry"));
//System.out.println(modifiedName.indexOf("rry", 4));
//System.out.println(modifiedName.lastIndexOf("rry", 7));
//System.out.println(name.equals("Harry"));
System.out.println(name.equalsIgnoreCase("HarRY"));
System.out.println("I am escape sequence\tdouble quote");
}
}