I have a string:
thorax1 [00400 - 00479]
su [00100 - 0022200su]
head1 [00100 - 00228]
head1 [00100 - 00228]
thorax1 [00400 - 00479]
lab66 [lab661]
this can be big also. I need to retrieve the value that is there is in square bracket,
i.e. 00400 - 00479, 00100 - 0022200su ,00100 - 00228
I have used:
String Lab1=thorax1 [00400 - 00479]
su [00100 - 0022200su]
head1 [00100 - 00228]
head1 [00100 - 00228]
thorax1 [00400 - 00479]
lab66 [lab661]
Lab1= Lab.substring(Lab.indexOf("[")+1,Lab.indexOf("]"));
but this is only giving me 00400 - 00479
I need the output like 00400 - 00479, 00100 - 0022200su ,00100 - 00228 and so on.
Can anyone help me to get the desire output?
The problem is that what you are trying to do works only once. You need to place that logic (and add some more) into a loop.
This should do what you need:
String Lab = "thorax1 [00400 - 00479]"
+ "su [00100 - 0022200su] "
+ "head1 [00100 - 00228] "
+ "head1 [00100 - 00228] "
+ "thorax1 [00400 - 00479] " +
"lab66 [lab661]";
int first = 0;
int last = 0;
while(true)
{
first = Lab.indexOf("[", first) + 1;
last = Lab.indexOf("]", first);
if(first <= 0)
{
break;
}
System.out.println(Lab.substring(first, last));
}
Yields:
00400 - 00479
00100 - 0022200su
00100 - 00228
00100 - 00228
00400 - 00479
lab661