Home>

Due to the need to intercept strings in the project,Then hand disabled used the substr automatically suggested by ide. I didn't think so much that substr and substring were not much different.

But it ’s not,Listen to me one by one.

1. substr (index, length)

Extracts a specified number of characters from a string from the starting index number.

The substr method can pass two parameters, index and length. index is the starting bit,length is the truncated length.

When index is a non-negative integer:

a. If no parameters are passed in,What you get is the string itself.

Example:"abcdefg" .substr ()->"abcdefg"

b. If only one parameter is passed,That is, the index is intercepted to the last bit of the string by default.

For example:"abcdefg" .substr (2)->"defg".

When index is a negative integer:

c. If a negative integer subscript bit is passed in, it counts from back to front,The starting bit is 1, the subscript bit that reaches the absolute value of the negative integer,The interception was from after.

Example:"abcdefg" .substr (-2)->"fg"<=>"abcdefg" .substr (-2 + "abcdefg" .length)

2. substring (start, end)

Extracts characters in a string between two specified indices.

substring is also passed two parameters,But these two parameters are subscript numbers,The subscript is calculated from 0,The intercepted length is the difference between these two subscripts

Can be recorded as "with head and no tail"

Example:"abcdefg" .substring (2,4)->"cd"

  • Previous Explain the use of basic class libraries and language packs in Java
  • Next Detailed Java file and directory management and input-output related operations