Home  >  Article  >  Java  >  How to get a substring using String.substring() method in Java?

How to get a substring using String.substring() method in Java?

WBOY
WBOYOriginal
2023-11-18 08:07:561346browse

How to get a substring using String.substring() method in Java?

How to get a substring using the String.substring() method in Java?

The String class in Java provides a very useful method substring(), which can be used to obtain the substring of a string. It allows us to select a portion of characters from a string and return it as a new string. This article will explain how to use the substring() method in Java and provide some code examples.

Using the substring() method is very simple. It has two overloaded forms:

  1. substring(int beginIndex): Starting from the specified starting index (including this index), Intercept the last part of the original string and return a new string.
  2. substring(int beginIndex, int endIndex): Starting from the specified starting index (including the index) to the specified end index (excluding the index), intercept a part of the original string and return a new string.

The following is some sample code using the substring() method:

// Example 1: intercept the back part of the string
String str = "Hello World";
String subStr = str.substring(6);
System.out.println(subStr); // Output: World

// Example 2: Intercept part of the string
String str = "Hello World";
String subStr = str.substring(0, 5);
System.out.println(subStr); // Output: Hello

// Example 3: Get The last character of the string
String str = "Hello World";
String lastChar = str.substring(str.length() - 1);
System.out.println(lastChar); // Output: d

// Example 4: Extract the username part of the email address
String email = "example@example.com";
int atIndex = email.indexOf("@");
String username = email.substring(0, atIndex);
System.out.println(username); // Output: example

You need to pay attention to the following points when using the substring() method :

  1. The starting index beginIndex must be a non-negative integer and less than or equal to the string length. Otherwise an IndexOutOfBoundsException exception will be thrown.
  2. End index endIndex must be a non-negative integer, greater than or equal to the starting index, and less than or equal to the string length. Otherwise an IndexOutOfBoundsException exception will be thrown.
  3. If the end index is not specified, the substring() method will intercept all characters from the starting index to the end of the string.
  4. In most cases, endIndex is an open range, that is, it does not include the character at the end index position. At this time, the length of the substring returned by the substring() method is equal to endIndex minus beginIndex.
  5. For scenarios where you need to extract a part of the characters in a string, you can specify the starting index and ending index.

To sum up, the String.substring() method in Java is a very useful method that can be used to obtain the substring of a string. We can use it to intercept part of the string according to specific needs. Hopefully the code examples provided in this article will help you better understand and use the substring() method.

The above is the detailed content of How to get a substring using String.substring() method in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn