-4

I want to get substring from starting line third comma(delimeter) and from ending line second comma(delimeter).

eg:- input:

"8884052344,89,\N,11805143\,3\,6112555\,6538937\,5839176\,5420860\,6590106\,2646753,0,2015-09-29 10:52:29"

Output:

11805143\,3\,6112555\,6538937\,5839176\,5420860\,6590106\,2646753

Note: From the starting line third comma and from ending line second comma substring may increase the size.Please help me.

Thanks.....

2 Answers2

0

You can cut the string by the "," and than make a new string, read this How do I use a delimiter in Java Scanner?

Community
  • 1
  • 1
Dusius
  • 75
  • 2
  • 13
0

Will the String always have more than 5 commas ? I have assumed that it will , We can solve it in two ways.

First approach

I have used indexOf to determine the index of third occurrence of "," and lastIndexOf to determine the second last occurrence of ",".

String ex= "8884052344,89,jhgN,11805143,3,6112555,6538937,5839176,5420860,6590106,2646753,0,2015-09-29 10:52:29";
int beginningIndex= ex.indexOf(',',(ex.indexOf(',',ex.indexOf(',')))+1)+1;
int lastIndex= ex.lastIndexOf(',',ex.lastIndexOf(',')-1);
String desiredResult=ex.substring(beginningIndex,lastIndex);

Other approach .

you can just split the String , you will get an array . create a StringBuffer and append the String from the index 3 till the second last index ( array length - 3 ) .