Substring in Java

Java is one of the most popular programming languages in the world. It is a high-level, object-oriented language that is widely used for developing web applications, mobile applications, and desktop applications. One of the key features of Java is its ability to work with strings. In this article, we will focus on the substring method in Java.

What is a substring?

A substring is a part of a string. It is a sequence of characters that are contained within a larger string. For example, the string “Hello World” contains the substring “World”. Substrings are useful when you want to extract a specific portion of a string or when you want to manipulate a string in a certain way.

What is the substring() method in Java?

The substring() method is a built-in method in Java that is used to extract a substring from a string. It is a part of the String class and takes two arguments: the starting index and the ending index of the substring. The starting index is the position of the first character in the substring, and the ending index is the position of the last character in the substring.

The syntax of the substring() method is as follows:

String substring(int startIndex)
String substring(int startIndex, int endIndex)

The first version of the substring() method takes only one argument, which is the starting index of the substring. It returns a new string that starts from the specified index and goes to the end of the original string.

The second version of the substring() method takes two arguments: the starting index and the ending index of the substring. It returns a new string that starts from the specified starting index and ends at the specified ending index. The character at the ending index is not included in the substring.

Here is an example that demonstrates the use of the substring() method:

String str = "Hello World";
String sub1 = str.substring(6); // sub1 = "World"
String sub2 = str.substring(0, 5); // sub2 = "Hello"

In this example, we create a string “Hello World” and use the substring() method to extract two substrings. The first substring (sub1) starts from the 6th index and goes to the end of the string, which is “World”. The second substring (sub2) starts from the 0th index and goes to the 5th index, which is “Hello”.

Things to keep in mind when using the substring() method:

The starting index and the ending index are both zero-based. This means that the first character in the string has an index of 0, the second character has an index of 1, and so on.

If the starting index is greater than the ending index, an exception will be thrown.

If the ending index is greater than the length of the string, the substring will go to the end of the string.

The substring() method returns a new string and does not modify the original string.

Conclusion:

The substring() method is a powerful tool in Java that allows you to extract a specific portion of a string. It is easy to use and provides a lot of flexibility when working with strings. Whether you are building a web application, a mobile application, or a desktop application, the substring() method can help you manipulate strings in a variety of ways.

1 Comment

Comments are closed