Substring () method in java with example

Priscilla 126 Published: 12/10/2024

Substring () method in java with example

I'd be happy to help!

The substring() method in Java is used to return a new string that is a subset of the original string. This method takes two parameters: the starting index (inclusive) and the ending index (exclusive).

Here's an example:

Let's say we have a string "hello world" and we want to get the substring from the 6th character to the end.

String str = "hello world";

int start = 5; // 0-based indexing, so 5 is the 6th character

int end = str.length(); // until the end of the string

String subStr = str.substring(start, end);

System.out.println(subStr); // Output: world

In this example, we're calling substring(5, str.length()), which returns "world".

If you only provide one parameter, it's equivalent to calling substring(0, thatParameter). For example:

String subStr = str.substring(5);

System.out.println(subStr); // Output: world

This is equivalent to str.substring(5, str.length()).

Here are some more examples:

Get the first three characters of a string:
String str = "abcdefg";

int start = 0;

int end = 3;

String subStr = str.substring(start, end);

System.out.println(subStr); // Output: abc

Get all but the last character of a string:
String str = "abcdefg";

int start = 0;

int end = str.length() - 1;

String subStr = str.substring(start, end);

System.out.println(subStr); // Output: abcdef

Note that if start is greater than or equal to end, the method throws a StringIndexOutOfBoundsException.

In summary, the substring() method in Java allows you to extract a subset of characters from an original string, which can be useful for various tasks such as parsing data, extracting specific information, and more.

I hope this helps!

Substring () method in java w3schools

I'm happy to help! However, I need to clarify that W3Schools is a website that provides tutorials and references for web development, not Java-specific information. Nevertheless, I can provide you with an explanation of the substring() method in Java.

The substring() method in Java is used to return a new string that is a subset of the original string, starting from a specified index and ending at another specified index. The syntax for this method is:

public String substring(int beginIndex)

or

public String substring(int beginIndex, int endIndex)

The first version of the method returns a substring that starts from the character at the given beginIndex and goes until the end of the string.

The second version of the method returns a substring that starts from the character at the given beginIndex and ends at the character at the given endIndex. If endIndex is greater than or equal to the length of the original string, it will return a substring that goes until the end of the string.

Here's an example:

String str = "Hello, World!";

String sub = str.substring(7);

System.out.println(sub); // Output: World!

In this example, we're asking for a substring starting from index 7 (which is the character 'W') until the end of the string. The output will be "World!".

String str = "Hello, World!";

String sub = str.substring(7, 12);

System.out.println(sub); // Output: World

In this example, we're asking for a substring starting from index 7 (which is the character 'W') and ending at index 11 (which is the character '!'). The output will be "World".

Note that if you try to create a substring with an endIndex that is less than the beginIndex, you'll get a StringIndexOutOfBoundsException.

The substring() method is commonly used when working with strings, such as extracting specific parts of text, validating user input, or processing data. It's a powerful tool in your Java toolkit!