Usage of substring in Java

substring

1.public String substring(int beginIndex)。 Returns a new string, which is a substring of this string. The substring starts with the character at the specified index and continues to the end of the string.

Parameters: beginindex - index at the beginning (including).

Returns: the specified substring.

For example: "unhappy" substring(2) returns "happy"   "Harbison". substring(3) returns "bison"   "emptiness". substring(9) returns "" (an empty string)  2. public String substring(int beginIndex,int length)。 Returns a new string, which is a substring of this string. The substring starts from the specified beginindex. Length: indicates the length of the substring.

Parameters: beginindex - index at the beginning (including). Index at the end (excluding).

Returns: the specified substring.

Example: "hamburger" substring(4,8) returns "urge"   "smiles". substring(1,5) returns "mile"

three

The above returned string: "ell"; Str.substring (1,1) / / return e

Str.substring (1) / / return "ello world";

In addition, strange phenomena will appear in this function. When str.substring (5,0) appears; What's the matter? But the returned value is "hello", str.substring (5,1) / / the returned value is "ello". The first bit is truncated and the rest is returned

It can be seen that substring (start, end) can have different descriptions, that is, start can be the length to be returned, and end is the number of characters to be removed (starting from the first)

In JS, substr (start, length) is easy to use

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>