Java String Functions – 25+ MUST Know Methods

Filed Under: Java
Java String Functions

Java String Functions

  • Java String class has a lot of functions to manipulate strings.
  • There are many methods to get the characters and bytes of the string object.
  • There are methods to split the string into an array or to create substrings.
  • Since String is immutable, the original string remains unchanged and a new instance of string is returned.

Java String Functions List

Here is the list of most important String class functions. The list is updated for Java 13 release.

  • length()
  • isEmpty(), isBlank()
  • charAt()
  • getChars(), toCharArray()
  • getBytes()
  • equals(), hashCode() and equalsIgnoreCase()
  • contentEquals()
  • compareTo() and compareToIgnoreCase()
  • startsWith() and endsWith()
  • indexOf() and lastIndexOf()
  • substring() and subSequence()
  • concat()
  • matches()
  • replace(), replaceFirst(), and replaceAll()
  • contains()
  • split()
  • join()
  • toLowerCase() and toUpperCase()
  • trim(), strip(), stripLeading(), and stripTrailing()
  • lines()
  • indent()
  • transform()
  • format()
  • intern()
  • valueOf() and copyValueOf()
  • repeat()
  • describeConstable() and resolveConstantDesc()
  • formatted(), stripIndent(), and translateEscapes()

Let’s look into these methods with simple code snippets running in JShell.

1. length()

String length() method returns the length of the string object.


jshell> String s = "Hello Java";
s ==> "Hello Java"
 
jshell> s.length()
$2 ==> 10

2. isEmpty(), isBlank()

String isEmpty() method returns True if the string is empty i.e. length is 0.

String isBlank() method was added to the String class in Java 11 release. This method returns True if the string is empty or contains only whitespace characters such as spaces and tabs.


jshell> String emptyStr = "";
emptyStr ==> ""
 
jshell> String whitespacesStr = " \t\t ";
whitespacesStr ==> " \t\t "
 
jshell> emptyStr.isEmpty()
$5 ==> true
 
jshell> whitespacesStr.isEmpty()
$6 ==> false
 
jshell> emptyStr.isBlank()
$7 ==> true
 
jshell> whitespacesStr.isBlank()
$8 ==> true

3. charAt(int index)

This method returns the character at the given index. If the index is out of range, StringIndexOutOfBoundsException is thrown.


jshell> String s = "Java";
s ==> "Java"
 
jshell> s.charAt(2)
$10 ==> 'v'
 
jshell> s.charAt(20)
|  Exception java.lang.StringIndexOutOfBoundsException: String index out of range: 20
|        at StringLatin1.charAt (StringLatin1.java:48)
|        at String.charAt (String.java:709)
|        at (#11:1)

4. getChars() and toCharArray()

String getChars() method is used to populate a character array from the string object as source. The method syntax is:


getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
  1. The srcBegin parameter is the index of the first character to copy from the string.
  2. The srcEnd is the index after the last character in the string to copy.
  3. The dst[] is the destination character array.
  4. The dstBegin parameter is the offset index in the destination char array.

jshell> String s1 = "12345678";
s1 ==> "12345678"
 
jshell> char[] dest = new char[5];
dest ==> char[5] { '\000', '\000', '\000', '\000', '\000' }
 
jshell> s1.getChars(2, 5, dest, 1)
 
jshell> System.out.println(Arrays.toString(dest));
[, 3, 4, 5, ]

Java String toCharArray() method returns a new character array created from the string characters.


jshell> String s2 = "12345";
s2 ==> "12345"
 
jshell> s2.toCharArray()
$28 ==> char[10] { '1', '2', '3', '4', '5' }

5. getBytes()

This method returns byte array created from the string. There are three versions of getBytes() method.

  1. getBytes(): this method decode the bytes using the system default character set encoding.
  2. getBytes(Charset charset): The character set is used to decode the bytes.
  3. getBytes(String charsetName): The charsetName is used to get the actual Charset instance for decoding. If the charset is invalid, UnsupportedEncodingException is thrown.

jshell> String s3 = "Hello";
s3 ==> "Hello"
 
jshell> s3.getBytes();
$21 ==> byte[5] { 72, 101, 108, 108, 111 }
 
jshell> import java.nio.charset.StandardCharsets;
 
jshell> s3.getBytes(StandardCharsets.UTF_16);
$22 ==> byte[12] { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0, 111 }
 
jshell> s3.getBytes("UTF-16");
$23 ==> byte[12] { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0, 111 }
 
jshell> s3.getBytes("UTF-8");
$24 ==> byte[5] { 72, 101, 108, 108, 111 }

6. equals(), hashCode() and equalsIgnoreCase()

String equals(Object obj) method returns true if and only if the object is a string and represents the same sequence of characters as this string.

The equals() method is being overridden from the Object class. The equals() method is used in Hashtable implementations.


jshell> String s4 = "Hello"
s4 ==> "Hello"
 
jshell> s4.equals(new Object())
$26 ==> false
 
jshell> s4.equals("Hello")
$28 ==> true

The hashCode() method returns the integer hash value of the string object. It’s cached when the string is created.

The formula to calculate the hash code is given below.


s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

Here s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation.


jshell> String s = "Java"
s ==> "Java"
 
jshell> s.hashCode()
$30 ==> 2301506
 
jshell> String s2 = "Java"
s2 ==> "Java"
 
jshell> s2.hashCode()
$32 ==> 2301506
 
jshell> "String".hashCode()
$33 ==> -1808118735

If you are wondering why the hashCode() value is negative, it’s because the value returned from the formula is larger than the maximum value of the integer. So it’s causing integer overflow and in that case, the hashcode can be negative.

String equalsIgnoreCase(String str) compares this string with the given string, ignoring case considerations.


jshell> "Java".equalsIgnoreCase("JAVA")
$35 ==> true
 
jshell> "Java".equalsIgnoreCase("JA")
$36 ==> false

7. contentEquals()

This String method compares the content of the string with the CharSequence object. It’s used to compare the string with the StringBuilder and StringBuffer object values.


jshell> String str = "12345"
str ==> "12345"
 
jshell> StringBuffer sb = new StringBuffer()
sb ==> 
 
jshell> sb.append("12345")
$38 ==> 12345
 
jshell> str.contentEquals(sb)
$39 ==> true
 
jshell> StringBuilder sb1 = new StringBuilder(sb)
sb1 ==> 12345
 
jshell> str.contentEquals(sb1)
$41 ==> true

8. compareTo() and compareToIgnoreCase()

String implements Comparable interface. So, we can compare two strings lexicographically using the compareTo() method.

The compareToIgnoreCase() method also performs the lexicographical comparison, ignoring case.


jshell> "Java".compareTo("Java")
$42 ==> 0
 
jshell> "Java".compareTo("Jb")
$43 ==> -1
 
jshell> "Java".compareToIgnoreCase("JAVA")
$44 ==> 0
 
jshell> "Java".compareToIgnoreCase("JB")
$45 ==> -1

9. startsWith() and endsWith()

These methods are used to check if the string has given prefix or suffix strings or not. The startsWith() has an overloaded method to provide the integer value as the offset index.


jshell> "123456".startsWith("123")
$46 ==> true
 
jshell> "123456".startsWith("34")
$47 ==> false
 
jshell> "123456".startsWith("34", 2)
$48 ==> true
 
jshell> "123456".endsWith("456")
$49 ==> true
 
jshell> "123456".endsWith("45")
$50 ==> false

10. indexOf() and lastIndexOf()

There are four overloaded indexOf() methods in String class.

  1. indexOf(int ch): returns the first index of the character in the string. If the character is not present, then returns -1.
  2. indexOf(int ch, int fromIndex): the second parameter specifies the index from where to search for the character.
  3. indexOf(String str): returns the index of the first occurrence of the substring.
  4. indexOf(String str, int fromIndex): returns the index of the first occurrence of the substring starting from the given index.

jshell> "Java".indexOf('a')
$51 ==> 1
 
jshell> "Java".indexOf('a', 2)
$52 ==> 3
 
jshell> "Java".indexOf("av")
$53 ==> 1
 
jshell> "Java".indexOf("av", 2)
$54 ==> -1

Similarly, there are four overloaded lastIndexOf() methods. The behavior is similar, except that the last index is returned. The search for the character or the substring starts from the end of the string.

  1. lastIndexOf(int ch)
  2. lastIndexOf(int ch, int fromIndex): the search is performed from backward, the returned index is between 0 and fromIndex.
  3. lastIndexOf(String str)
  4. lastIndexOf(String str, int fromIndex)

jshell> "Java".lastIndexOf('a')
$55 ==> 3
 
jshell> "Java".lastIndexOf('a', 2)
$56 ==> 1
 
jshell> "Java".lastIndexOf("av")
$57 ==> 1
 
jshell> "Java".lastIndexOf("av", 3)
$58 ==> 1

11. substring() and subSequence()

Java String substring() method is used to create a substring from this string. The subSequence() method also creates a substring and internally calls the substring() method.


jshell> String str = "Java String"
str ==> "Java String"
 
jshell> str.substring(5)
$60 ==> "String"
 
jshell> str.substring(5, 8)
$61 ==> "Str"
 
jshell> str.subSequence(5, 8)
$62 ==> "Str"

12. concat()

String concat() method concatenates this string with the given string and returns it. The original string remains unchanged and a new string object reference is returned.


jshell> "Java".concat("String")
$63 ==> "JavaString"

13. matches()

This method is used for regular expression pattern matching. If the string matches the given regex, it returns true.


jshell> String str = "Java"
str ==> "Java"
 
jshell> str.matches("^J.v.")
$67 ==> true
 
jshell> str.matches("\\D{4}")
$68 ==> true

14. replace(), replaceFirst(), and replaceAll()

These methods are used to replace part of the string with the given character or substring. There are four methods to replace string characters.

  1. replace(char oldChar, char newChar)
  2. replaceFirst(String regex, String replacement)
  3. replaceAll(String regex, String replacement)
  4. replace(CharSequence target, CharSequence replacement)

15. contains()

This method returns true if the string contains the given character sequence.


jshell> "Java".contains("av")
$69 ==> true
 
jshell> "Java".contains(new StringBuilder("Ja"))
$70 ==> true

16. split()

This method is used to split the string around the matches of the given regex. There are two variations of this method.

  1. split(String regex, int limit): The limit parameter defines the number of records in the result string array.
  2. split(String regex): returns the string array with all the substrings around the regex matches.

Recommended Reading: Java String split() Method

17. join()

This static method was added to the String class in Java 8 release. It is used to concatenate a group of strings with the given delimiter to form a new string. It’s very useful in creating CSV string from a list or array of strings.


jshell> String.join(",", "A", "B", "C");
$1 ==> "A,B,C"

Recommended Reading: Java String join() Method

18. toLowerCase() and toUpperCase()

These methods are used to create lowercase and uppercase strings from this string. There are overloaded methods to specify the Locale to be used for the conversion.


jshell> "Java".toUpperCase()
$71 ==> "JAVA"
 
jshell> "JavA".toLowerCase()
$72 ==> "java"
 
jshell> "Java".toUpperCase(Locale.US)
$73 ==> "JAVA"
 
jshell> "JavA".toLowerCase(Locale.UK)
$74 ==> "java"

Recommended Readings:

19. trim(), strip(), stripLeading(), and stripTrailing()

Java String trim() method returns a new string after stripping all the leading and trailing whitespaces. This method considers any character whose codepoint is less than or equal to ‘U+0020’ as a whitespace character.

String strip() method was added to the String class in Java 11 release. This method uses Character.isWhitespace() method to remove leading and trailing whitespaces from a string. This is the recommended method to remove whitespaces from a string object.

The stripLeading() and stripTrailing() methods remove leading and trailing whitespaces respectively.


jshell> String str = "  \t Hi  \t"
str ==> "  \t Hi  \t"
 
jshell> str.trim()
$76 ==> "Hi"
 
jshell> str.strip()
$77 ==> "Hi"
 
jshell> str.stripLeading()
$78 ==> "Hi  \t"
 
jshell> str.stripTrailing()
$79 ==> "  \t Hi"

Recommended Reading: Java 11: New Methods in String Class

20. lines()

This method returns a stream of lines extracted from this string, separated by line terminators.


jshell> String str = "Hi\nHello\nYes\r\nNo\n";
str ==> "Hi\nHello\nYes\r\nNo\n"
 
jshell> List lines = new ArrayList<>();
lines ==> []
 
jshell> str.lines().forEach(s -> lines.add(s));
 
jshell> System.out.println(lines);
[Hi, Hello, Yes, No]

21. indent()

This method is used to indent every line in the string and normalize the newline characters. It was added to String API in Java 12 release.

Recommended Reading: Java 12 String Methods

22. transform()

This method is used to apply a function to this string. The function should accept a single string argument and return an object. Some of the real-world usages are to create a list of strings from a CSV string and transforming a string to a list of objects. This utility method was added to String API in Java 12 release.

23. format()

Java String format() method returns a formatted string using the given format and the arguments. It’s inspired by the C language sprintf() method.

24. intern()

This method returns the canonical representation of the string. It’s a native method. When a string is created using new operator, it gets created outside the string pool. The intern() method moves the string to the pool and returns the reference.

Recommended Reading: Java String intern() Method

25. valueOf() and copyValueOf()

Java String valueOf() method is used to get the string representation of the argument. There are various overloaded valueOf() methods that accept primitive data types, char array, and object. The String valueOf() methods are static.

26. repeat()

Java String repeat() method returns a new string whose value is the concatenation of this string given number of times. This method was added to the String class in Java 11 release.


jshell> String str = "123"
str ==> "123"

jshell> str.repeat(3)
$9 ==> "123123123"

27. describeConstable() and resolveConstantDesc()

These methods are added to support Constants’ API for the String class. These methods were added to the String class in Java 12 release.


jshell> String s = "Hi"
s ==> "Hi"
 
jshell> s.describeConstable()
$85 ==> Optional[Hi]
 
jshell> import java.lang.invoke.MethodHandles;
 
jshell> s.resolveConstantDesc(MethodHandles.lookup());
$87 ==> "Hi"

Reference: Java 13 String API Docs

Comments

  1. Harvansh Singh says:

    You are missing one most important method chars() that’s returns intStream object

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages