- length() – Returns the length of String name. (5 in this case)
- toLowerCase() – Returns a new String which has all the lowercase characters from the String name.
- toUpperCase() – Returns a new String which has all the uppercase characters from the String name.
- trim() – Returns a new String after removing all the leading and trailing spaces from the original string.
- substring(int start) – Returns a substring from start to the end. Substring(3) returns “ry”. [Note that index starts from 0]
- 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.
- replace(‘r’, ‘p’) – Returns a new string after replacing r with p. Happy is returned in this case. (This method takes char as argument)
- startsWith(“Ha”) – Returns true if name starts with string “Ha”. (True in this case)
- endsWith(“ry”) – Returns true if name ends with string “ry”. (True in this
case) - charAt(2) – Returns the character at a given index position. (r in this case)
- indexOf(“s”) – Returns the index of the given string.
For e.g. name.indexOf(“ar”) returns 1 which is the first occurrence of ar in string “Harry”, -1 otherwise.
- indexOf(“s”, 3) – Returns the index of the given String starting from index 3(int). (-1 is returned in this case)
- lastIndexOf(“r”) – Returns the last index of the given string. (3 in this case)
- lastIndexOf(“r”,2) – Returns the last index of the given string before index 2.
- equals(“Harry”) – Returns true if the given string is equal to “Harry” false otherwise [Case sensitive]
- 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.
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");
}
}

Comments
Post a Comment