Reputation: 1269
I need the most efficient way to parse the following string and extract the imgurl from it using java.
{ms:"images,5160.1",
turl:"http://ts1.mm.bing.net/th?id=I4693880201938488&pid=1.1",
height:"178",width:"300",
imgurl:"http://www.attackingsoccer.com/wp-content/uploads/2011/07/World-Cup-2012-Draw.jpg",
offset:"0",t:"World Cup 2014 Qualification – Europe Draw World Cup 2012 Draw ...",
w:"719",h:"427",ff:"jpeg",
fs:"52",durl:"www.attackingsoccer.com/2011/07/world-cup-2012-qualification-europe...",
surl:"http://www.attackingsoccer.com/2011/07/world-cup-2012-qualification-europe-draw/world-cup-2012-draw/",
mid:"D9E91A0BA6F9E4C65C82452E2A5604BAC8744F1B",k:"6",ns:"API.images"}"
For the above string the output should be :
http://www.attackingsoccer.com/wp-content/uploads/2011/07/World-Cup-2012-Draw.jpg
Any help is appreciated.
Thanks!
Upvotes: 0
Views: 274
Reputation: 6987
You can also use a Pattern
and a Matcher
:
String string = "something....,imgurl=\"blabla\",somethinother";
Pattern p = Pattern.compile(",imgurl=\"[^\"]*\",");
Matcher m = p.matcher(string);
m.find();
String result = m.group().subSequence(1, m.group().length() - 1).toString();
This will work even if there are other places in the string where the text "imgurl:" or ".jpg" appears.
Upvotes: 0
Reputation: 828
Seems like it's JSON message. You can convert this into POJO using e.g. GSON.
Upvotes: 3
Reputation: 91
String str = YOUR STRING;
String startStr = "imgurl:¥"";
String endStr = ".jpg";
String value = str.subString(str.indexOf(startStr) + startStr.length , str.indexOf(endStr) + endStr.length);
Upvotes: 0