Java String Method


The java String class provides serveral methods in string such as compare(), concat(), equals(), length(), replace(), compareTo(), substring(), and etc.

There are commonly used string methods in Java. They are

  • String concat(String str): It concatenates str2 at the end of string str1.
  • char charAt(int where): It returns the character at a specified location.
  • boolean equals(Object str): It is used to compare two strings for equality.
  • boolean equalsIgnore(String str): It is used to compare two strings for equality but it ignores case differences.
  • indexOf(): It is used to searches for the first occurrence of a character value.
  • lastIndexOf(): It is used to searches for the last occurrence of a character value.
  • int compareTo(String str): It is used to compares the given string with the invoking string.
  • length(): It is used to find the length of the string. It returns the count number of characters.
  • capacity(): It is used to find the total allocated capacity of string.
  • append(): It concatenates str2 at the end of string str1.
  • delete(): It is used to delete a sequence of characters from the invoking object.
  • reverse(): It returns the reverse object when it was invoked.
  • replace(): It replaces one set of characters with another set of characters.
  • substring(): It returns a portion of the StringBuffer.
  • trim(): It is used to remove leading and trailing whitespace.

Example:

public class Stringmethod{  
    public static void main(String args[]){  
        String s1="Welcome"; 
        s1=s1.concat(" to Onlinetpoint");  
        System.out.println(s1);  
    }
} 



Onlinetpoint is optimized for basic learning, practice and more. Examples are well checked and working examples available on this website but we can't give assurity for 100% correctness of all the content. This site under copyright content belongs to Onlinetpoint. You agree to have read and accepted our terms of use, cookie and privacy policy.