String Class Methods In Java

String Class Methods In Java | The java.lang.String class given in Java programming language contains many built-in methods for string manipulation and string handling. In this blog, we will discuss those string class methods in Java with examples. If you want to be a good Java programmer then String class methods are essential and they will help you a lot while coding.

String class methods in Java to find the length, fetch the character at some given index, finding the index of the given characters are as follows:-

MethodsDescription
int length()Returns the length of the given string.
char charAt(int index)Returns character at the given index.
int indexOf(-)Gives the first index of a given character or string either from start or from the given position.
int lastIndexOf(-)Gives the last index of a given character or string either from start or from the given position.

There are many variations of indexOf() and lastIndexOf() methods. The four overloaded forms of the indexOf() methods are:-

  • public int indexOf(int ch)
  • public int indexOf(int ch, int fromIndex)
  • public int indexOf(String str)
  • public int indexOf(String str, int fromIndex)

The four overloaded forms of the lastIndexOf() methods are:-

  • public int lastIndexOf(int ch)
  • public int lastIndexOf(int ch, int fromIndex)
  • public int lastIndexOf(String str)
  • public int lastIndexOf(String str, int fromIndex)
public class Main {
    public static void main(String[] args) {

        String string = "Hello World";
        System.out.println(string.length()); // 11
        
        System.out.println(string.charAt(0)); // 'H'
        System.out.println(string.charAt(6)); // 'W'
        
        System.out.println(string.indexOf('l')); // 2
        System.out.println(string.indexOf('l', 5)); // 9
        
        System.out.println(string.lastIndexOf('o')); // 7
        System.out.println(string.lastIndexOf('o', 5)); // 4
    }
}

Output:-

11
H
W
2
9
7
4

String class methods in Java to check whether a string is empty, blank, contains some character, starts or ends with some specific character/string, or not.

MethodsDescription
boolean isEmpty()Returns true if, and only if, length() is 0.
boolean isBlank()It returns true if the string is empty or contains only white space codepoints, otherwise returns false.
boolean contains(CharSequence s)Returns true if and only if this string contains the specified sequence of char values.
boolean startsWith(String prefix)Test if this string starts with the specified prefix.
boolean startsWith(String prefix, int toffset)Tests if the substring of this string beginning at the specified index starts with the specified prefix.
boolean endsWith(String suffix)Test if this string ends with the specified suffix.

Program to demonstrate String class methods in Java,

public class Main {
    public static void main(String[] args) {
        String string = "Know Program";
        System.out.println(string.isEmpty()); // false
        System.out.println(string.isBlank()); // false

        System.out.println(string.startsWith("K")); // true
        System.out.println(string.startsWith("m")); // false
        System.out.println(string.startsWith("Know")); // true
        System.out.println(string.startsWith("Program")); // false

        System.out.println(string.endsWith("K")); // false
        System.out.println(string.endsWith("m")); // true
        System.out.println(string.endsWith("Know")); // false
        System.out.println(string.endsWith("Program")); // true
    }
}

Output:-

false
false
true
false
true
false
false
true
false
true

Like any other classes in Java, String is also a child class of java.lang.Object class. And string class overrides some methods of Object class like hashCode(), toString(), and equals() methods.

MethodsDescription
int hashCode()Returns a hash code for this string.
String toString()Return the string itself.
boolean equals(Object anObject)Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
boolean equalsIgnoreCase(String anotherString)Same as the above equals() method, ignoring case considerations.
int compareTo()Compares two strings lexicographically.
int compareToIgnoreCase()Compares two strings lexicographically, ignoring case considerations.
boolean contentEquals(StringBuffer sb)Compares this string to the specified StringBuffer.
boolean contentEquals(CharSequence cs)Compares this string to the specified CharSequence.
public class Main {
    public static void main(String[] args) {
        String str1 = "Know Program";
        String str2 = "Hello World!";

        System.out.println(str1.hashCode());
        System.out.println(str2.hashCode());

        System.out.println(str1.toString());
        System.out.println(str2.toString());
    }
}

Output:-

1608858095
-969099747
Know Program
Hello World!

public class Main {
    public static void main(String[] args) {

        String str1 = "Know Program";
        String str2 = "know program";
        String str3 = "KP";

        System.out.println(str1.equals(str2));
        System.out.println(str1.equals(str3));
        System.out.println(str1.equalsIgnoreCase(str2));
        System.out.println(str1.equalsIgnoreCase(str3));

        System.out.println(str1.compareTo(str2));
        System.out.println(str1.compareTo(str3));
        System.out.println(str1.compareToIgnoreCase(str2));
        System.out.println(str1.compareToIgnoreCase(str3));
    }
}

Output:-

false
false
true
false
-32
30
0
-2

public class Main {
    public static void main(String[] args) {
        String str = "Know Program";
        StringBuffer sb = new StringBuffer("Know Program");
        CharSequence cs = new StringBuilder("Know Program");
        System.out.println(str.contentEquals(sb));
        System.out.println(str.contentEquals(cs));
    }
}

Output:-

true
true

MethodsDescription
void getChars(int srcBegin, int srcEnd, char dst[ ], int dstBegin)Copies characters from this string into the destination character array.
byte[ ] getBytes(String charsetName)Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.
byte[ ] getBytes(Charset charset)Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array.
char[ ] toCharArray()Converts this string to a new character array.
String substring(int beginIndex)Returns a string that is a substring of this string.
String substring(int beginIndex, int endIndex)Returns a string that is a substring of this string.
CharSequence subSequence(int beginIndex, int endIndex)Returns a character sequence that is a subsequence of this sequence.
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String str = "Know Program";
        char[] ch = new char[str.length()];

        str.getChars(0, str.length(), ch, 0);
        System.out.println(Arrays.toString(ch));
    }
}

Output:-

[K, n, o, w, , P, r, o, g, r, a, m]

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) throws UnsupportedEncodingException {
        String string = "Know Program";

        byte[] byteArray1 = string.getBytes("UTF-16");
        System.out.println(Arrays.toString(byteArray1));

        byte[] arrayUTF8 = string.getBytes(StandardCharsets.UTF_8);
        System.out.println(Arrays.toString(arrayUTF8));
    }
}

Output:-

[-2, -1, 0, 75, 0, 110, 0, 111, 0, 119, 0, 32, 0, 80, 0, 114, 0, 111, 0, 103, 0, 114, 0, 97, 0, 109]
[75, 110, 111, 119, 32, 80, 114, 111, 103, 114, 97, 109]

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String string = "Know Program";
        char[] charArray = string.toCharArray();
        System.out.println(Arrays.toString(charArray));
    }
}

Output:-

[K, n, o, w, , P, r, o, g, r, a, m]

public class Main {
    public static void main(String[] args) {
        String string = "Know Program";
        System.out.println(string.substring(5));
        System.out.println(string.substring(5, 8));
        System.out.println(string.subSequence(8, string.length()));
    }
}

Output:-

Program
Pro
gram

String class methods in Java to join or concatenate strings together or repeat some string multiple times are as follows:-

MethodsDescription
String join(CharSequence delimiter, CharSequence… elements)Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
String concat(String str)Concatenates the specified string to the end of this string.
String repeat(int count)Returns a string whose value is the concatenation of this string repeated count times.
public class Main {
    public static void main(String args[]) {
        String string = String.join(".", "www", "knowprogram", "com");
        System.out.println(string);

        String string1 = "Know".concat("Program");
        System.out.println(string1);

        String string2 = "www".concat(".")
                              .concat("knowprogram")
                              .concat(".")
                              .concat("com");
        System.out.println(string2);

        String string3 = "Hi".repeat(3);
        System.out.println(string3);
    }
}

Output:-

www.knowprogram.com
KnowProgram
www.knowprogram.com
HiHiHi

String class methods to replace or remove some character or substring from the given string is as follows:-

MethodsDescription
String replace(char oldChar, char newChar)Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
String replaceFirst(String regex, String replacement)Replaces the first substring of this string that matches the given regular expression with the given replacement.
String replaceAll(String regex, String replacement)Replaces each substring of this string that matches the given regular expression with the given replacement.

Both replace() and replaceAll() methods seem to replace all occurrences but they are different from each other. See the difference between replace() and replaceAll() methods in Java. Let us see an example of it.

public class Main {
    public static void main(String args[]) {
        String string = "Hello World";

        String string1 = string.replace('l', '@');
        System.out.println(string1);

        String string2 = string.replaceFirst("l", "#");
        System.out.println(string2);

        String string3 = string.replaceAll("o", "9");
        System.out.println(string3);
    }
}

Output:-

[email protected]@o [email protected]
He#lo World
Hell9 W9rld

MethodsDescription
String[ ] split(String regex)Splits this string around matches of the given regular expression.
String[ ] split(String regex, int limit)Splits this string around matches of the given regular expression.
String toUpperCase()Converts all of the characters in this String to upper case using the rules of the default locale.
String toUpperCase(Locale locale)Converts all of the characters in this String to upper case using the rules of the given Locale.
String toLowerCase()Converts all of the characters in this String to lowercase using the rules of the default locale.
String toLowerCase(Locale locale)Converts all of the characters in this String to lower case using the rules of the given Locale.
import java.util.Arrays;

public class Main {
    public static void main(String args[]) {
        String string = "Hi How are you?";

        String[] array1 = string.split(" ");
        System.out.println(Arrays.toString(array1));

        String[] array2 = string.split(" ", 2);
        System.out.println(Arrays.toString(array2));

        System.out.println(string.toUpperCase());
        System.out.println(string.toLowerCase());
    }
}

Output:-

[Hi, How, are, you?]
[Hi, How are you?]
HI HOW ARE YOU?
hi how are you?

MethodsDescription
String trim()Returns a string whose value is this string, with all leading and trailing space removed, where space is defined as any character whose codepoint is less than or equal to ‘U+0020’ (the space character).
String strip()Returns a string whose value is this string, with all leading and trailing white space removed.
String stripLeading()Returns a string whose value is this string, with all leading white space removed.
String stripTrailing()Returns a string whose value is this string, with all trailing white space removed.
String stripIndent()Returns a string whose value is this string, with incidental white space removed from the beginning and end of every line.

We have discussed strip(), stripLeading(), stripTrailing(), and stripIndent() in detail. Also, see the difference between the trim() and strip() methods in Java. Let us see an example of the above methods.

public class Main {
    public static void main(String args[]) {
        String string = "  Hi How are you?    ";
        System.out.println("Original length: " + string.length());

        String str1 = string.trim();
        System.out.println(str1.length());

        String str2 = string.strip();
        System.out.println(str2.length());

        String str3 = string.stripLeading();
        System.out.println(str3.length());

        String str4 = string.stripTrailing();
        System.out.println(str4.length());

        String str5 = string.stripIndent();
        System.out.println(str5.length());
    }
}

Output:-

Original length: 21
15
15
19
17
15

MethodsDescription
static String valueOf(-)Returns the string representation of the given argument.
static String copyValueOf(char data[ ])Equivalent to valueOf(char[ ]).
static String copyValueOf(char data[ ], int offset, int count)Equivalent to valueOf(char[ ], int, int).
static String format(String format, Object… args)Returns a formatted string using the specified format string and arguments.
static String format(Locale l, String format, Object… args)Returns a formatted string using the specified locale, format string, and arguments.
String formatted(Object… args)Formats using this string as the format string, and the supplied arguments.

There are many overloaded forms of valueOf() method which is used to convert primitive, char arrays and objects to String format. The format() method is very similar to Java printf() method, and C language printf() method. We have discussed the String.format() method with examples in detail.

public class Main {
    public static void main(String args[]) {
        int num = 10;
        System.out.println(String.valueOf(num));
        System.out.println(String.valueOf(99.999));
        System.out.println(String.valueOf(true));
        System.out.println(String.valueOf(new char[] { 'K', 'n', 'o', 'w' }));
    }
}

Output:-

10
99.999
true
Know

String class methods for codePoints(), codePointAt(), codePointBefore(), codePointCount(), and offsetByCodePoints()

MethodDescription
public IntStream codePoints()Returns a stream of code point values from this sequence.
public int codePointAt(int index)Returns the character (Unicode code point) at the specified index.
public int codePointBefore(int index)Returns the character (Unicode code point) before the specified index.
public int codePointCount(int beginIndex, int endIndex)Returns the number of Unicode code points in the specified text range of this String.
public int offsetByCodePoints(int index, int codePointOffset)Returns the index within this String that is offset from the given index by codePointOffset code points.

Other methods of the String class,

MethodDescription
Stream<String> lines()Returns a stream of lines extracted from this string, separated by line terminators.
String indent(int n)Adjusts the indentation of each line of this string based on the value of n, and normalizes line termination characters.
String translateEscapes()Returns a string whose value is this string, with escape sequences translated as if in a string literal.
<R> R transform(Function<? super String, ? extends R> f)This method allows the application of a function to this string. The function should expect a single String argument and produce an R result.
IntStream chars()Returns a stream of int zero-extending the char values from this sequence. Any char which maps to a surrogate codepoint is passed through uninterpreted.
String intern()Returns a canonical representation for the string object.
Optional<String> describeConstable()Returns an Optional containing the nominal descriptor for this instance, which is the instance itself.
String resolveConstantDesc(MethodHandles.Lookup lookup)Resolves this instance as a ConstantDesc, the result of which is the instance itself.
boolean matches(String regex)Tells whether or not this string matches the given regular expression.
boolean regionMatches(int toffset, String other, int ooffset, int len)Test if two string regions are equal.
boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)Test if two string regions are equal.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.