EN
Java - get substring from string
0 points
In this article, we would like to show you how to get a substring of the string in Java.
Quick solution:
xxxxxxxxxx
1
String text = "abcd";
2
String substring = text.substring(0, 2); // start from index 0 up to 2
3
System.out.println(substring); // ab
In this example, we present different cases of how to get a substring from the text
.
xxxxxxxxxx
1
public class Example {
2
public static void main(String[] args) {
3
String text = "abcd";
4
5
String substring1 = text.substring(0, 2); // start from index 0 to 2
6
String substring2 = text.substring(1, 3); // start from index 1 to 3
7
String substring3 = text.substring(0); // from index 0 to the end of the string
8
9
System.out.println(substring1); // ab
10
System.out.println(substring2); // bc
11
System.out.println(substring3); // abcd
12
}
13
}