Marjer
Marjer

Reputation: 1403

Split String based on the String and patterns - Updated

String prop="AS=AP:BS=BP:CS=CS:DS=DPS:";
String currentValue="AS"

I have the above string value(prop), i want to split the String based on the String value(currentValue) and print the value between the Equal and Colon sign.
So i have used the below code to achieve this, but its failing in for the last string value "DSP" Since it's length is 3

System.out.println(prop.substring((prop.indexOf(geo)+3), (prop.indexOf(geo)+5)));

I'm an Unix Engineer used awk to achive this in a UNIX script, Is there any way to achieve this as a one liner in JAVA?

Update : the below code is working fine but its failing when both values are equal, like AS=AS:BS=BS:CS=CP:DS=DPS: when currentValue is AS/BS its failing String subStr=prop.split(currentValue)[1]; System.out.println(subStr.substring(subStr.indexOf("=")+1,subStr.indexOf(":")));

Upvotes: 0

Views: 110

Answers (1)

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35577

Are you looking this kind of solution?

String prop="AS=AP:BS=BP:CS=CP:DS=DPS:";
String currentValue="AS";

String subStr=prop.split(currentValue)[1]; 
System.out.println(subStr.substring(subStr.indexOf("=")+1,subStr.indexOf(":")));

Upvotes: 1

Related Questions