Java String substring() method

The Java substring() method retrieves a segment of characters from a string. It generates a fresh string, while the initial string remains unaltered due to its immutability in Java.

Methods in Java for extracting a substring from a String.

The Java String substring method has two versions with overloaded variations.

    The “substring” method of a string returns a new string that is a portion of the original string. The first variation of the method starts from a specified index and includes all characters until the end of the original string. The second variation of the method starts from a specified beginning index and includes all characters up to, but not including, the character at the specified end index.

Key points about the significance of the String substring() method.

    1. The IndexOutOfBoundsException may be thrown by either of the substring methods if any of the conditions listed below are met:

– When beginIndex is a negative number
– When endIndex exceeds the length of the String object
– When beginIndex is larger than endIndex

Both substring methods treat beginIndex as inclusive and endIndex as exclusive.

Example of using the Java String substring() method.

I have a straightforward Java program for extracting substrings.

package com.scdev.util;

public class StringSubstringExample {

	public static void main(String[] args) {
		String str = "www.scdev.com";
		System.out.println("Last 4 char String: " + str.substring(str.length() - 4));
		System.out.println("First 4 char String: " + str.substring(0, 4));
		System.out.println("website name: " + str.substring(4, 14));
	}
}

The result of the program mentioned above is the output of the substring example.

Last 4 char String: .com
First 4 char String: www.
website name: scdev

Using the substring() method to verify if a word or phrase is a palindrome.

To determine if a String is a palindrome or not, we have the option to utilize the substring() method.

package com.scdev.util;

public class StringPalindromeTest {
	public static void main(String[] args) {
		System.out.println(checkPalindrome("abcba"));
		System.out.println(checkPalindrome("XYyx"));
		System.out.println(checkPalindrome("871232178"));
		System.out.println(checkPalindrome("CCCCC"));
	}

	private static boolean checkPalindrome(String str) {
		if (str == null)
			return false;
		if (str.length() <= 1) {
			return true;
		}
		String first = str.substring(0, 1);
		String last = str.substring(str.length() - 1);
		if (!first.equals(last))
			return false;
		else
			return checkPalindrome(str.substring(1, str.length() - 1));
	}
}

We’re determining whether the first and last letter are identical. If they’re not, the result is false. Otherwise, we recurse the method, passing in the substring without the first and last letter.

You can find additional string examples in our GitHub Repository.

Citation: Oracle API Documentation

 

more tutorials

Python Substring refers to extracting a smaller portion of a string.(Opens in a new browser tab)

Spring MVC HandlerInterceptorAdapter and HandlerInterceptor.(Opens in a new browser tab)

convert string to character array in Java.(Opens in a new browser tab)

multithreading in Java that you need to know(Opens in a new browser tab)

Leave a Reply 0

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