String Methods in Java

1. Table of Different String Methods.

MethodsDescription
char charAt(int index)Returns the character at specified index. The index value should be 0 to length ()-1.
int compareTo(Object o)Compares the string and string object based on Unicode value.
int compareTo(String string)Compare between two strings lexicographically means alphabetically.
int compareToIgnoreCase(String str)Compare two strings lexicographically, but it does not consider case while comparing.
boolean contentEquals(StringBuffer sb)Compare the strings to the specified string buffers. It returns the boolean value.
static String copyValueof(char[ ] data)Returns a string that represents the character sequence.
boolean endsWith(String suffix)Returns “true” if the given index ends with string suffix otherwise “false”.
boolean equals(Object O)This method is used to compare this string to specified object.
boolean equalsIgnoreCase(String str)Compare two strings while comparing case consideration.
boolean matches(String regex)Check whether the string matches the specified regular expression.
String toLowerCase()Converts all the characters in this string to lower case.
String toLowerCase(Locale locale)Converts all the character in lower case using rules of given locale.
String toUpperCase()Converts total characters of the given string in upper case.
String toUpperCase(Locale locale)Converts all the characters in upper case according to given locale.

2. Table of Different String Methods.

MethodsDescription
byte getBytes()Returns the byte array of string in the sequence of the bytes.
void getChars(int srcBeg, int scrEnd, char[ ] dest, int destBeg)Copy characters from a string into the destination character array.
int hashCode()Returns hashcode of the String.
int indexOf(int ch)Returns index with a string of the first occurrence of the specified character.
int indexOf(int ch, int fromIndex)Returns the index to the string of the first occurrence of the specified character, starting the search at specified index.
int indexOf(String str)Returns index position of the given substring.
int indexOf(String str, int fromIndex)This method returns the position to the given substring and from given index.
String intern()Returns canonical representation of string object.
int isEmpty()This method returns true if the length of the string is zero.
int lastIndexOf(int ch)Returns the last occurrence of the character ch in the string.
int lastIndexOf(int ch, int fromIndex)Returns the last occurrence of the character, but it starts searching from given index.
int lastIndexOf(String str)Returns the last occurrence of the sub-string from the given string.
int lastIndexOf(String str, int fromIndex)Returns the last index position for the given substring from given index value.
int length()Returns total number of characters in a string.
String replace(char oldChar, char newChar)Returns the new string after replacing old character with new character.
String replaceAll(String regexp, String replacement)Returns the new string replacing all the sequence of the characters matching regular expression and replacement string.
String replaceFirst(String regexp, String replacement)Replace first substring of given string that matches the given regular expression.
String[] split(String regexp)Splits the given string according to match of regular expression.
String[] split(String regexp, int limit)Splits the string and returns the array of substring.
boolean startsWith(String prefix)Returns true, If given string starts with specified string prefix.
boolean  startsWith(String prefix, int toffset)Checks the given string beginning at the specified index start with the specified prefix.
String substring(int beginIndex)Returns the new string that is substring of that string.
String substring(int beginIndex, int endIndex)Returns the substring of the given string. In this method begin index is inclusive and end index is exclusive.
String[] toCharArray()Converts the given string to a new character array.
String trim()Return the copy of the string after eliminating leading and trailing spaces.
Static String valueOf(primitive data type)Convert different types of primitive values into string.


Example 1 : Simple program to illustrate the uses of String method

public class StringDemo
{
      public static void main(String args[])
      {
            String s1 = "TutorialRide";
            String s2 = "Java Tutorial";
            // check string is empty or not
            System.out.println(s1.isEmpty());
            //find the length of string
            System.out.println(s1.length());
            System.out.println(s2.trim());
            System.out.println(s1.concat(".com"));
            System.out.println(s2.replace('J','V'));
      }
}


Output:
false
12
Java Tutorial
TutorialRide.com
Vava Tutorial

Example 2 : Progarm to illustrate different comparisons method on string method

public class StringDemo
{
       public static void main(String args[])
       {
             String s1 = "Hello";
             String s2 = "hello";
             String s3 = new String("ABC");
             String s4 = new String ("ABC");
             String s5 = new String("Abc");
             //Comparing two String
             System.out.println(s1 == s2);
             System.out.println(s1.equals(s2));
             System.out.println(s1.equalsIgnoreCase(s2));
             System.out.println();
             System.out.println(s3 == s4);
             System.out.println(s3.equals(s4));
            System.out.println();
            System.out.println(s3 == s5);
            System.out.println(s3.equals(s5));
            System.out.println();
            //String compareTo() method
            System.out.println(s1.compareTo(s2));
            System.out.println(s1.compareToIgnoreCase(s2));
            System.out.println(s3.compareTo(s4));
            System.out.println(s3.compareTo(s5));
       }
}


Output:
false
false
true

false
true

false
false

-32
0
0
-32

Example 3 : Program to illustrate different method in String

public class StringDemo
{
        public static void main(String args[])
        {
             String s1 = "Java Programming Tutorial";
             String s2 = new String("Hello");
             System.out.println(s1.startsWith("Java"));
             System.out.println(s1.startsWith("java"));
             System.out.println(s1.startsWith("Programming"));
             System.out.println(s1.endsWith("Tutorial"));
             System.out.println(s1.endsWith("Java"));
             System.out.println();
             //charAt() and length()
             for (int i = 0; i < s2.length(); i++)
              {
                   System.out.println("Character at index "+ i + " is : "+s2.charAt(i));
              }
        }
}


Output:
true
false
false
true
false

Character at index 0 is : H
Character at index 1 is : e
Character at index 2 is : l
Character at index 3 is : l
Character at index 4 is : o

Example 4 : A program to reverse the word of the string object

public class ReverseStringDemo
{
       public static String reverseString(String s)
       {
            String[] str1 = s.split(" ");
            String result1 = "";
            for (int i = str1.length-1; i >= 0; i--)
            {
                   result1 += str1[i]+" ";
            }
            return result1;
       }
       public static void main(String args[])
       {
             String str = "Welcome Java";
             String result = ReverseStringDemo.reverseString(str);
             System.out.println("Given String: "+str);
             System.out.println("Reverse String: "+result);
       }
}


Output:
Given String: Welcome Java
Reverse String: Java Welcome