String Functions In Java

Various methods are defined in the String class for manipulating with the sequence of characters.

String Functions In Java

Various methods are defined in the String class for manipulating with the sequence of characters.

String Functions In Java

String Functions In Java

In this section we will see various methods of String class that are defined for various purposes. We will use some of them methods in a simple example.

String class is belongs to the java.lang package. It is a final class i.e. it can not be extended. Because the String class is defined as final so its object are immutable however, String buffers are mutable. This class has defined various of methods to perform operations on strings. Some of these methods are as follows :

  • char charAt(int index) : This method is used to get the character at the given index of a given String.

    Syntax : public char charAt(int index)
     
  • int codePointAt(int index) : This method is used to find the Unicode of character at the given index of a String.

    Syntax : public int codePointAt(int index)
     
  • int codePointBefore(int index) : This method is used to find the Unicode of character before the given index of a String.

    Syntax : public int codePointBefore(int index)
     
  • int codePointCount(int beginIndex, int endIndex) : This method is used to get the number of Unicode points in the given text range of index of a String.

    Syntax : public int codePointCount(int beginIndex, int endIndex)
     
  • int compareTo(String anotherString) : This method compares two strings using Unicode value of every characters. It compares the sequence of characters of Strings lexicographically.

    Syntax : public int compareTo(String anotherString)
     
  • int compareToIgnoreCase(String str) : This method compares two strings using Unicode value of every characters. It compares the sequence of characters of Strings lexicographically by ignoring case. The returned value of this method may be a negative integer, zero, or a positive integer that demonstrates the comparing string is greater than, equal to or, less than the compared String respectively.

    Syntax : public int compareToIgnoreCase(String str)
     
  • String concat(String str) : This method is used to append the given string at the end of the string.

    Syntax : public String concat(String str)
     
  • boolean contains(CharSequence s) : This method is used to check whether the given sequence of char values is contained by the current String or not.

    Syntax : public boolean contains(CharSequence s)
     
  • boolean contentEquals(CharSequence cs) : This method is used to compare the String with the given sequence of char.

    Syntax : public boolean contentEquals(CharSequence cs)
     
  • boolean contentEquals(StringBuffer sb) : This method is used to compare the String with the given StringBuffer.

    Syntax : public boolean contentEquals(StringBuffer sb)
     
  • static String copyValueOf(char[] data) : This method is used to get the String from the given array of characters.

    Syntax : public static String copyValueOf(char[] data)
     
  • static String copyValueOf(char[] data, int offset, int count) : This method is used to get the String from the given array of characters started from the specified index and the number of characters.

    Syntax : public static String copyValueOf(char[] data, int offset, int count)
     
  • boolean endsWith(String suffix) : This method is used to check whether the string is ends with the given string.

    Syntax : public boolean endsWith(String suffix)
     
  • boolean equals(Object anObject) : This method is used to check whether the String object is equal to the given Object or not.

    Syntax : public boolean equals(Object anObject)
     
  • boolean equalsIgnoreCase(String anotherString) : This method is used to check by ignoring the case of characters, whether the String object is equal to the the given String object or not.

    Syntax : public boolean equalsIgnoreCase(String anotherString)
     
  • static String format(Locale l, String format, Object... args) : This method is used to format a String using the given Locale, format string and arguments.

    Syntax : public static String format(Locale l, String format, Object... args)
     
  • static String format(String format, Object... args) : This method is used to format a String using the given format string and arguments.

    Syntax : public static String format(String format, Object... args)
     
  • byte[] getBytes() : This method is used to get the array of bytes after encoding the String.

    Syntax : public byte[] getBytes()
     
  • byte[] getBytes(Charset charset) : This method is used to get the array of byte after encoding the String with the given charset.

    Syntax : public byte[] getBytes(Charset charset)
     
  • int hashCode() : This method is used to get the hash code of String.

    Syntax : public int hashCode()
     
  • int indexOf(int ch) : This method is used to get the position of the first occurrence of given character inside the String.

    Syntax : public int indexOf(int ch)
     
  • int indexOf(int ch, int fromIndex) : This method is used to get the position of the first occurrence of given character inside the String. In this method search operation is started from the given position.

    Syntax : public int indexOf(int ch, int fromIndex)
     
  • int indexOf(String str) : This method is used to get the position of the first occurrence of given substring inside the String.

    Syntax : public int indexOf(String str)
     
  • int indexOf(String str, int fromIndex) : This method is used to get the position of the first occurrence of given substring inside the String. In this method search operation is started from the given position.

    Syntax : public int indexOf(String str, int fromIndex)
     
  • boolean isEmpty() : This method is used to check whether the String has 0 (zero) length or not.

    Syntax : public boolean isEmpty()
     
  • int lastIndexOf(int ch) : This method is used to get the last occurrence position of the given character inside a String.

    Syntax : public int lastIndexOf(int ch)
     
  • int lastIndexOf(int ch, int fromIndex) : This method is used to get the last occurrence position of the given character inside a String. In this method the search is started from the given index.

    Syntax : public int lastIndexOf(int ch, int fromIndex)
     
  • int lastIndexOf(String str) : This method is used to get the last occurrence position of the given substring inside a String.

    Syntax : public int lastIndexOf(String str)
     
  • int lastIndexOf(String str, int fromIndex) : This method is used to get the last occurrence position of the given substring inside a String. In this method the search operation is started from the given index.

    Syntax : public int lastIndexOf(String str, int fromIndex)
     
  • int length() : This method is used to get the number of characters within the String.

    Syntax : public int length()
     
  • boolean matches(String regex) : This method is used to match the string with the given regular expression.

    Syntax : public boolean matches(String regex)
     
  • String replace(char oldChar, char newChar) : This method is used to replace the given old character with the given new character.

    Syntax : public String replace(char oldChar, char newChar)
     
  • String replaceAll(String regex, String replacement) : This method is used to replace all substring of the String if the given regular expression is matched with the given replacement.

    Syntax : public String replaceAll(String regex, String replacement)
     
  • String[] split(String regex) : This method is used to split the string from the specified matching regular expression.

    Syntax : public String[] split(String regex)
     
  • String[] split(String regex, int limit) : This method is used to split the string form the specified matching regular expression with the specified limit.

    Syntax : public String[] split(String regex, int limit)
     
  • boolean startsWith(String prefix) : This method is used to check whether the string is started with the given string or not.

    Syntax : public boolean startsWith(String prefix)
     
  • String substring(int beginIndex) : This method is used to get the string within the String called substring. Substring is started from the given index.

    Syntax : public String substring(int beginIndex)
     
  • String substring(int beginIndex, int endIndex) : This method is used to get the string within the String called substring. This Substring is a part of the string specified within the starting index and end index.

    Syntax : public String substring(int beginIndex, int endIndex)
     
  • char[] toCharArray() : This method is used to convert a string into an array of characters.

    Syntax : public char[] toCharArray()
     
  • String toLowerCase() : This method is used to convert the upper case characters to lower case.

    Syntax : public String toLowerCase()
     
  • String toString() : This method is used to get the String itself.

    Syntax : public String toString()
     
  • String toUpperCase() : This method is used to convert the lower case characters to upper case.

    Syntax : public String toUpperCase()
     
  • String trim() : This method is used to omit the whitespace around the String.

    Syntax : public String trim()
     
  • static String valueOf(boolean b) : This method is used to represent a boolean as a String.

    Syntax : public static String valueOf(boolean b)
     
  • static String valueOf(Object obj) : This method is used to represent an object as a String.

    Syntax : public static String valueOf(Object obj)
     

Note : Many methods in String class are overloaded. Here we have not discussed all methods of String class. To read all methods of String class please refer to Java doc.

Example

Here an example is being given which will demonstrate you about how to use methods of String class in Java. In this example I will create a Java file where I will use some of the methods of String class.

StringFunctionsExample.java


public class StringFunctionsExample {

	public static void main(String[] args) {

		String str1 = "RoseIndia";
		String str2 = "roseIndia";
		String str3 = "Delhi";
		String str4 = "india";
		
		System.out.println("1. Third character of String '"+str1+"' : "+str1.charAt(2));
		System.out.println("2. Length of String '"+str1+"' : "+str1.length());
		System.out.println("3. Unicode of "+str1.charAt(str1.length()-1)+" : "+str1.codePointAt(str1.length()-1));
		
		int compare = str1.compareToIgnoreCase(str2);		
		if(compare == 0)
		{
			System.out.println("4. "+str1+" and "+str2+" both string is same when compare them and ignoring case differences");
		}
		else if(compare > 0 )
		{
			System.out.println("4. "+str1+" is less than "+str2);
		}
		else
		{
			System.out.println("4. "+str1+" is greater than "+str2);
		}
		System.out.println("5. After Concating "+str1+" and "+str3+" : "+str1.concat(str3));
		boolean bol = str1.endsWith("India");
		System.out.println("6. Whether the string '"+str1+"' has suffix '"+str4+"' : "+bol);
		
		System.out.println("7. Index of character "+str1.charAt(str1.length()-2)+" in String '"+str1+"' : "+str1.indexOf(str1.charAt(str1.length()-2)));
		System.out.println("Converting "+str1+" to lower case : "+str1.toLowerCase());
		System.out.println("Converting "+str2+" to upper case : "+str1.toUpperCase());		
	}

}

Output

When you will compile and execute the above example you will get the output as follows :

Download Source Code