Important Methods in String class

Here most common used String Methods Discussed Here


  • charAt() - Returns the Character located at at the specified index for Example :
    String x="Computer";
    System.out.println(x.charAt(2)); // output is 'm'

  • concat() - Appends one String to the end of another ("+" also works), ex:
    String x="motor";
    System.out.println(x.concat(" cab"));
    // output is "motor cab"

  • equalIgnoreCase() - Determines the equality of two Strings,ignoring case.
    String x = "HERO"
    System.out.println(x.equalIgnoreCase("Hero"));
       //is "true"
    System.out.println(x.equalIgnoreCase("reho"));    is "false"

  • length() - Returns the number of characters in a String.
    String x="Computer"
    System.out.println(x.length());     
    //     is 8

  • replace() - Replaces occurences of character with a new character

  • substring () - Returns a part of String
    String x = "0123456789"
    System.out.println(x.substring(5));     // output is "56789"
    System.out.println(x.substring(5,8));     //output is "567"

  • toLowerCase() - Returns a String with uppercase characters converted
    String x= "COMPUTERS";

    System.out.println(x.lowerCase());   //output is "computers"

  • toString() - returns the value of String
    String x = "Rama";
    System.out.println(x.toString());     // for your exercise buddy...

  • toUpperCase() - Returns a String with lowercase characters converted
    String x= "science";

    System.out.println(x.lowerCase());   //output is "SCIENCE"

  • trim() - Removes white spaces from ends of the String
    String x = " hi ";
    System.out.println(x + "x");    // result is " hi x"
    System.out.println(x.trim() + "x");    // result is " hix"

No comments:

Post a Comment