简体   繁体   中英

how can i split a string inside curly braces?

I have a string which looks like this

[{"TransactionId":"3574780600039252015-12-24 T 14:22:03"

Now I want to split only the text where the "TransactionId" will be part one and after : will be the second part.

Code I've tried :

String[] transid_result = result.split(":");
String part1 = transid_result[0];
String part2 = transid_result[1];

The result is

  • part1 contains [{"TransactionId
  • part2 contains "3574780600039252015-12-24 T 14

I want part2 to contain "3574780600039252015-12-24 T 14:22:03"

Can anyone help me ?

You could search for the first : and manually split by that:

int firstColon = result.indexOf(":");
String part1 = result.substring(0,firstColon);
String part2 = result.substring(firstColon+1, result.length());

If you're sure the data will always be in this format, then you could use

String[] elements = result.split("\"");
String transactionId = elements[3];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM