Home>Article>Java> Java gets the first few characters of a string

Java gets the first few characters of a string

angryTom
angryTom Original
2019-11-11 14:31:10 14976browse

Java gets the first few characters of a string

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!

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
Previous article:what is reflection in java Next article:what is reflection in java