java takes the first few characters of a string
The String class in Java provides asubstring (int from, int to)method is used to intercept the characters from the position from to to-1 in the string.
Because the character position of the string starts from 0, and the substring(int from, int to) method is closed in the front and open in the back, that is, [from, to), which can be understood as [from, to- 1].
At the same time, to can be omitted (polymorphism). By default, to intercepts the last digit of the string. (Recommended tutorial:java tutorial)
String str = "m.sbmmt.com"; System.out.println(str.substring(0, 3)); // www
Also note that String in Java is an immutable type, so the substring() method does not change the original string, but A new string is returned.
System.out.println(str); // m.sbmmt.com
The above is the detailed content of Java gets the first few characters of a string. For more information, please follow other related articles on the PHP Chinese website!