Java String Methods

Here is the list of the methods available in the Java String class. These methods are explained in separate tutorials with the help of examples. Links to the tutorials are provided below:

  1. char charAt(int index): returns the character at the specified index.
  2. boolean equals(Object obj): Compares the string with the specified string and returns a true or false value.
  3. boolean equalsIgnoreCase(String string): It works the same as equals method but it doesn’t consider the case while comparing strings. It does a case insensitive comparison.
  4. int compareTo(String string): This method compares the two strings based on the Unicode value of each character in the strings.
  5. int compareToIgnoreCase(String string): Same as CompareTo method however it ignores the case during the comparison.
  6. boolean startsWith(String prefix, int offset): It checks whether the substring (starting from the specified offset index) is having the specified prefix or not.
  7. boolean startsWith(String prefix): It tests whether the string is having specified prefix, if yes then it returns true else false.
  8. boolean endsWith(String prefix): Checks whether the string ends with the specified suffix.
  9. int hashCode(): It returns the hash code of the string.
  10. int indexOf(int ch): Returns the index of the first occurrence of the specified character ch in the string.
  11. int indexOf(int ch, int fromIndex): Same as indexOf method however it starts searching in the string from the specified fromIndex.
  12. int lastIndexOf(int ch): It returns the last occurrence of the character ch in the string.
  13. int lastIndexOf(int ch, int fromIndex): Same as lastIndexOf(int ch) method, it starts search from fromIndex.
  14. int indexOf(String str): This method returns the index of the first occurrence of specified substring str.
  15. int last index of(String str): Returns the index of the last occurrence of string str.
  16. String substring(int beginIndex): It returns the substring of the string. The substring starts with the character at the specified index.
  17. String substring(int beginIndex, int endIndex): Returns the substring. The substring starts with character at beginIndex and ends with the character at the endIndex.
  18. String Concat(String str): CONCATENATES means it joins the two string and passed to the specified string.
  19. String replace(char oldChar, char new car): It returns the new updated string after changing all the occurrences of oldChar with the newChar.
  20. boolean contains(CharSequence s): It checks whether the string contains the specified sequence of char values. If yes then it returns true else false. It throws NullPointerException of ‘s’ is null.
  21. String toUpperCase(Locale locale) : Converts the string to upper case string using the rules defined by specified locale.
  22. String toUpperCase(): Equivalent to toUpperCase(Locale.getDefault()).
  23. public String intern(): This method searches the specified string in the memory pool and if it is found then it returns the reference of it, else it allocates the memory space to the specified string and assigns the reference to it.
  24. public boolean isEmpty(): This method returns true if the given string has 0 length. If the length of the specified Java String is non-zero then it returns false.
  25. public static String join(): This method joins the given strings using the specified delimiter and returns the concatenated Java String
  26. String replaceFirst(String regex, String replacement): It replaces the first occurrence of substring that fits the given regular expression “regex” with the specified replacement string.
  27. String replaceAll(String regex, String replacement): It replaces all the occurrences of substrings that fits the regular expression regex with the replacement string.
  28. String[] split(String regex, int limit): It splits the string and returns the array of substrings that matches the given regular expression. limit is a result threshold here.
  29. String[] split(String regex): Same as split(String regex, int limit) method however it does not have any threshold limit.
  30. String toLowerCase(Locale locale): It converts the string to lower case string using the rules defined by the given locale.
  31. public static String format(): This method returns a formatted Java String
  32. String toLowerCase(): Equivalent to toLowerCase(Locale. getDefault()).
  33. String trim(): Returns the substring after omitting leading and trailing white spaces from the original string.
  34. char[] toCharArray(): Converts the string to a character array.
  35. static String copyValueOf(char[] data): It returns a string that contains the characters of the specified character array.
  36. static String copyValueOf(char[] data, int offset, int count): Same as above method with two extra arguments – initial offset of subarray and length of subarray.
  37. void getChars(int srcBegin, int srcEnd, char[] dest, int destBegin): It copies the characters of src array to the dest array. Only the specified range is being copied(srcBegin to srcEnd) to the dest subarray(starting fromdestBegin).
  38. static String valueOf(): This method returns a string representation of passed arguments such as int, long, float, double, char and char array.
  39. boolean contentEquals(StringBuffer sb): It compares the string to the specified string buffer.
  40. boolean regionMatches(int srcoffset, String dest, int destoffset, int len): It compares the substring of input to the substring of specified string.
  41. boolean regionMatches(boolean ignoreCase, int srcoffset, String dest, int destoffset, int len): Another variation of regionMatches method with the extra boolean argument to specify whether the comparison is case sensitive or case insensitive.
  42. byte[] getBytes(String charsetName): It converts the String into a sequence of bytes using the specified charset encoding and returns the array of resulted bytes.
  43. byte[] getBytes(): This method is similar to the above method it just uses the default charset encoding for converting the string into a sequence of bytes.
  44. int length(): It returns the length of a String.
  45. boolean matches(String regex): It checks whether the String is matching with the specified regular expression regex.
  46. int codePointAt(int index): It is similar to the charAt method however it returns the Unicode code point value of specified index rather than the character itself.

1.JAVA STRING EXAMPLE

import java.util.*;
public class StringExample
{ 
public static void main(String args[])
{ 
String s1="java";
//creating string by java string literal 
char ch[]={'s','t','r','i','n','g','s'};
 String s2=new String(ch);
//converting char array to string 
String s3=new String("example");
//creating java string by new keyword
 System.out.println(s1);
 System.out.println(s2); 
System.out.println(s3);
 }
} 

OUTPUT:
java
strings
example

2.Example of string for String Concat(String str)

import java.util.*;
class string1
{ 
public static void main(String args[])
{ 
String s="Sachin"; 
s=s.concat(" Tendulkar"); 
System.out.println(s); 
} 
} 
Output:
Sachin Tendulkar

3.EXAMPLE OF STRING equal() method

import java.util.*;
class comparison
{
 public static void main(String args[])
{
 String s1="Sachin";
 String s2="SACHIN"; 
System.out.println(s1.equals(s2));//false
 System.out.println(s1.equalsIgnoreCase(s2));//true
 }
 } 
Output:
false
true

4.Example of String toUpperCase() and toLowerCase() method

import java.util.*;
class string12
{
public static void main(String args[])
{
String s="Sachin"; 
System.out.println(s.toUpperCase());//SACHIN 
System.out.println(s.toLowerCase());//sachin 
System.out.println(s);//Sachin(no change in original) 
}
}
 OUTPUT:
SACHIN
sachin
Sachin

5.Example of String trim() method

import java.util.*;
class string55
{
public static void main(String args[])
{

String s=" Sachin "; 
System.out.println(s);// Sachin 
System.out.println(s.trim());//Sachin
}
} 
OUTPUT: 
  Sachin  
Sachin

6.Example String startsWith() and endsWith() method

import java.util.*;
class stringstart
{
public static void main(String args[])
{

String s="Sachin"; 
System.out.println(s.startsWith("Sa"));//true 
System.out.println(s.endsWith("n"));//true 
}
}
OUTPUT:
true
true
Translate »