you can use split(regex)
on your String
String billerlist = "DOCOMOBH-TATA DOCOMO Mobile Bihar-SRSBL53-DOCOMOBH";
String[] array = billerlist.split("-");
if(array.length>2){
String thirdElement = array[2];
}
Question
I am very new to android..Can you please help me to solve this. I have a string like
DOCOMOBH-TATA DOCOMO Mobile Bihar-SRSBL53-DOCOMOBH
and I have to get the third element. i.e SRSBL53
I have done this.
StringTokenizer stringTokenizer = new StringTokenizer(billerlist, "-");
String strBillerId = stringTokenizer.nextToken();
Thus i have got the first element DOCOMOBH.but how to get the third one.Thanks.
Solution
you can use split(regex)
on your String
String billerlist = "DOCOMOBH-TATA DOCOMO Mobile Bihar-SRSBL53-DOCOMOBH";
String[] array = billerlist.split("-");
if(array.length>2){
String thirdElement = array[2];
}
OTHER TIPS
Try using TextUtils.StringSplitter
, a StringTokenizer
would have to read at least n tokens in order to determine which is the n-th one. Thus it might be easier to just create a string array using StringSplitter
TextUtils.StringSplitter splitter = new TextUtils.SimpleStringSplitter("-");
splitter.setString(billerList);
if(splitter.size>2){
String myString = splitter[2];
}
Optimized answer is provided by others but for the sake of answering what you are trying see the following code
String billerlist = "DOCOMOBH-TATA DOCOMO Mobile Bihar-SRSBL53-DOCOMOBH";
StringTokenizer stringTokenizer = new StringTokenizer(billerlist, "-");
String requiredData = null; //local variables must be initialized before using
int count = 1;
while(stringTokenizer.hasMoreTokens()) {
String data = stringTokenizer.nextToken();
if(count == 3) {
requiredData = data;
}
count++;
}
if(requiredData != null) {
System.out.println("Required data is " + requiredData);
}
else {
System.out.println("Required data not present");
}
and the output is
Required data is SRSBL53