13
Dec
You can get part of a string by following two methods:
- substring(index) : This is a method of removing string before the index position
- substring(0,index) : This is a method to return string from 0 index to its given index
Suppose, there is a string as, “Hello Java”
If you want to return only Hello, then you have to use substring(0,6). In this string, there are total 10 characters. substring(0,6) means, remove characters after 6th character. So it will return hello.
If you want to return Java, then you have to use substring(6). It will remove 1st 6 characters from the string and will return Java.
Program to evaluate above scenario:
public static void main(String[] args) {
String s="Hello Java";
System.out.println(s.substring(0,6));
System.out.println(s.substring(6));
}
Output:
Hello
Java
Leave a Reply