1. length() – Returns the length of String name. (5 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 “ry”. [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. Happy is returned in this case. (This method takes char as argument)
  8. startsWith(“Ha”) – Returns true if name starts with string “Ha”. (True in this case)
  9. endsWith(“ry”) – Returns true if name ends with string “ry”. (True in this

    case)
  10. charAt(2) – Returns the character at a given index position. (r in this case)
  11. 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

Popular posts from this blog

Creating a Thread by Extending Thread class

Constructors from Thread class in Java