Java Online Compiler
String Handling

Performing various operation on String value is known as String handling.In java language String can be handled with the help of following predefined classes.

  1. String class
  2. StringBuffer class
  3. StringBuilder class
  • String Class:
  • It is also predefiend class in java Language in java.lang package. Whose object is immutable( Value can not be changed )
    Syntax:
    String s="Value";
    or
    String objref=new String("Value");

    Example:
    String s1="Sateesh";
    String s2=new String("Sateesh");

    String class offers following methods to perform various operations on String value.
    String Length
    String

    which can be used to find number of characters of given string.
    Syntax: s.length();

                                        
    class Main
    {
    public static void main(String args[])
    {
    String s=new String("Sateesh");
    System.out.println(s.length());
    }
    }
    

    String CharAt

    Which can be used to find the cheracter at the given index value.
    Syntax: s.charAt(indexvalue);

    
    class Main
    {
    public static void main(String args[])
    {
    String s=new String("Sateesh");
    System.out.println(s.charAt(2));
    }
    }
    

    String CompareTo

    Which can be used to compare two strings.This method returns an integer value . It returns zero both the strings same otherwise it returns on zero integer value.
    syntax: int n=s1.compareTo(s2);

                                    
    class main
    {
    public static void main(String args[])
    {
    String s1=new String("sateesh");
    String s2=new String("SATEESH");
    int n=s1.compareTo(s2);
    if(n==0)
    {
    System.out.println("Both Are Equal");
    }
    else
    {
    System.out.println("Both Are Not Equal");
    }
    }
    }
    

    String CompareToIgnoreCase

    which also similar to compareTo method.But it is case sensitive method. that means both the strings are having same value with different case.That the JVM treats both are same strings.
    syntax: int n=s1.compareToCase(s2);

    class Main
    {
    public static void main(String args[])
    {
    String s1=new String("sateesh");
    String s2=new String("SATEESH");
    int n=s1.compareToIgnoreCase(s2);
    if(n==0)
    {
    System.out.println("Both Are Equal");
    }
    else
    {
    System.out.println("Both Are Not Equal");
    }
    }
    }
    

    String Concat

    Which can be used to combine two strings into a single string;
    syntax: s1.concat(s2);

    
    
    class Main
    {
    public static void main(String args[])
    {
    String s1=new String("sateesh");
    String s2=new String("kumar");
    System.out.println(s1.concat(s2));
    }
    }
    
    
    

    String StartsWith

    Which returns true if the string is starts with given substring otherwise return false
    syntax: s1.startsWith("substring");

    class Main
    {
    public static void main(String args[])
    {
    String s1=new String("Welcome To Java Class");
    System.out.println(s1.startsWith("Welcome"));
    }
    }
    
    
    

    String EndsWith

    Which returns true if the string is ends with given substring otherwise return false
    syntax: s1.endsWith("substring");

    class Main
    {
    public static void main(String args[])
    {
    String s1=new String("Welcome To Java Class");
    
    
    
    System.out.println(s1.endsWith("Welcome"));
    }
    }
    

    String Equals

    Which can be used to compare two strings(content).Which returns true both the strings are same otherwise returns false.
    syntax: s1.equals(s2);
    Note: It is case sensitive method.

    
    class Main
    {
    public static void main(String args[])
    {
    String s1=new String("sateesh");
    String s2=new String("SATEESH");
    
    if(s1.equals(s2))
    {
    System.out.println("Both Are Equal");
    }
    else
    {
    System.out.println("Both Are Not Equal");
    }
    }
    }
    

    String EqualsIgnoreCase

    Which can be used to compare two strings(content).Which returns true both the strings are same otherwise returns false.But here ignore case.
    syntax: s1.equalsIgnoreCase(s2);

    class Main
    {
    public static void main(String args[])
    {
    String s1=new String("sateesh");
    String s2=new String("SATEESH");
    
    if(s1.equalsIgnoreCase(s2))
    {
    System.out.println("Both Are Equal");
    }
    else
    {
    System.out.println("Both Are Not Equal");
    }
    }
    }
    
    Note: what is difference between equals() and == ?
    equals always comes the content whereas == compare the reference;
    excample: String s1="Hello";
    String s2=new String("Hellow");
    s1.equals(s2); -- true
    s1==s2; -- false (references of both the strings different)

    String IndexOf

    Which can be used to get the index value from the given string or character(first occurence)
    s1.indexOf('character');
    s1.indexOf("String");

    class Main
    {
    public static void main(String args[])
    {
    String s1=new String("sateesh");
    System.out.println(s1.indexOf('e'));
    }
    }
    

    String LastIndexOf

    Which can be used to get the index value from the given string or character(last occurence)
    s1.LastIndexOf('character');
    s1.LastIndexOf("String");

    
    class Main
    {
    public static void main(String args[])
    {
    String s1=new String("sateesh");
    System.out.println(s1.lastIndexOf('e'));
    }
    }
    
    

    String SubString

    Which can be used to get the substring or part of a string from the given string.

    1. substring(index): usedto get the substring starting with the index value to last character.
      excample: s.substring(index);
    2. class Main
      {
      public static void main(String args[])
      {
      String s1=new String("sateesh");
      System.out.println(s1.substring(2));
      }
      }
                                             
    3. substring("starting index,ending index): it can be used to get the specific value from given string.
      excample: s1.substring(starting index,ending index);
      class Main
      {
      public static void main(String args[])
      {
      String s1=new String("sateesh");
      System.out.println(s1.substring(2,5));
      }
      }
      

    String ToLowerCase

    Which can be used to convert upper case to lower case
    syntax: s1.tolowerCase();

    class Main
    {
    public static void main(String args[])
    {
    String s=new String("SATEESH");
    System.out.println(s.toLowerCase());
    }
    }
    

    String ToUpperCase

    Which can be used to convert lower case to upper case
    syntax:s1.tolowerCase();

    
    class Main
    {
    public static void main(String args[])
    {
    String s=new String("sateesh");
    System.out.println(s.toUpperCase());
    }
    }
    

    String Trim

    It is used to trim the starting and ending spaces from the given strings
    syntax: s1.trim();

    
    class Main
    {
    public static void main(String args[])
    {
    String s1=new String(" sateesh");
    System.out.println(s1.trim());
    }
    }
    
    

    String Buffer

    it is a predefined class in java.lang package.which can be used to perform various operations on string .But whose object is mutable( content of the StringBuffer can be changed)
    Syntax:

    StringBuffer objref=new StringBuffer("Value");
                                        

    String Insert

    which can be used to insert either a new characer or string at given index value. syntax:
    sb.insert(index,'char');

    class Main
    {
    public static void main(String args[])
    {
    StringBuffer s1=new StringBuffer("sateesh  ");
    System.out.println(s1.insert(7,'M'));
    }
    }
    
    sb.insert(index,"String");
    class Main
    {
    public static void main(String args[])
    {
    StringBuffer s1=new StringBuffer("sateesh  ");
    System.out.println(s1.insert(7,"kumar"));
    }
    }
    

    String Delete

    Which can be used to delete either character or string from the main string. syntax:
    delete(index);

    class Main
    {
    public static void main(String args[])
    {
    StringBuffer s1=new StringBuffer("sateesh  ");
    System.out.println(s1.delete(1);
    }
    }
    
    delete(start index,ending index);
    class Main
    {
    public static void main(String args[])
    {
    StringBuffer s1=new StringBuffer("sateesh  ");
    System.out.println(s1.delete(1,3));
    }
    }
    
    Note:In higher version of java delete(index) is replaced by deleteCharAt(index).

    String Append

    Which can be used to add a new String at the end of existing string.
    syntax: append("String value");

    
    class Main
    {
    public static void main(String args[])
    {
    StringBuffer s1=new StringBuffer("sateesh  ");
    System.out.println(s1.append("kumar"));
    }
    }
    
    

    String Replace

    Which can be used to replace a new String in to old string based on the index value. syntax: replace(starting index,ending index,"new String")

    class Main
    {
    public static void main(String args[])
    {
    StringBuffer s1=new StringBuffer("sateesh  ");
    System.out.println(s1.replace(1,7,"kumar"));
    }
    }
    
    

    String Builder

    It is a same as StringBuffer class but only difference is StringBuffer is synchronized where as StringBuilder is non synchronized. Syntax:

    StringBuilder objref=new StringBuilder("Value");
                                        
    StringBuilder supports all the methods of String and StringBuffer.