Concept of Sub string in Java

Substring in Java

String class in java have a feature of cutting a part of String and storing it in another string called as Substring.
For the same thing String class have two overloaded methods

public String substring(int start)

public String substring(int start,int end)

First version of method is used when we need to cut of a String from a certain point till last.
Second version is used to cut a certain part of String, from a certain point to another point.
The working of substring is like this.
start index is calculated on 0 index and end index is calculated based on 1 index.

Working of Substring.

So let say if i have word HamBurger.
String s="Hamburger";
I need to take out burger from this, i will use first version of substring.
Based on zero index, index of b is 3So substring operation would be like this.s=s.substring(3);

Now let say i have a String value "Education" and i need to take out Ducati from it.
String s="Education";

So for taking Ducati out of Education we require start and end index.start index - 0 indexend index- 1 indexSo for ducati start index will be 1(0 based) and end index will be 7(1 index).String s="education";s=s.substring(1,7);


Post a Comment