user1537092
user1537092

Reputation: 21

Parsing String Java

I got a String:

["4fd1cf1783353a15415","4ffecf87fcc40d110a965626"]

or

["4fd5f684815345","4fd6ef3e60a676854651","4fd83c33c19164512153"]

And I'd like to store every id (eg. 4fd5f684815345, 4fd6ef3e60a676854651, 4fd83c33c19164512153...) in a independant String (or ArrayList).

How to parse it, because the String can be dynamic (1,2,3 values or more)?

OR JSON Array Parsing:

My JSON

"idCards":[
"4fc52f95egvt418541515",
"4fd1d05454151541545115"
],

A part of my code:

msg3 = (JSONArray) myobject.get("idCards");
System.out.println(msg3.toJSONString()); 

The result:

[4fc52f95egvt418541515","4fd1d05454151541545115"]

I'd like this 2 values in 2 differents String.

Many thanks for your help!

Upvotes: 2

Views: 801

Answers (6)

cl-r
cl-r

Reputation: 1264

it would be more secure (because ',' may be a decimal separator) to split with ("\",\""), and not remove trailing " in replaceAll, here subtring do not parse all the string :

final String str = "[\"4fd5f684815345\",\"4fd6ef3e60a676854651\",\"4fd83c33c19164512153\"]";

final String strArray[] = str.substring(2, str.length() - 2).split("\",\"");
final ArrayList<String> al = new ArrayList<String>();

for (final String string : strArray) {
    al.add(string);
    System.out.println(string);
}
System.out.println(al);    

for (final String string : strArray) { System.out.println(string); }

Output :

4fd5f684815345
4fd6ef3e60a676854651
4fd83c33c19164512153
[4fd5f684815345, 4fd6ef3e60a676854651, 4fd83c33c19164512153]

Upvotes: 0

nhahtdh
nhahtdh

Reputation: 56829

I make a number of assumptions here:

  • Assume no spaces before and after the delimiting [, ], ,
  • Assume no , and " character in the Strings you want to extract

    input.substring(1, input.length() - 1).replaceAll("\\\"", "").split(",");
    

    Or if you don't want to mess with regular expression (replaceAll function works with regular expression), then you can use replace method:

    input.substring(1, input.length() - 1).replace("\"", "").split(",");
    

Due to the assumptions above, this answer is very brittle. Consider using JSON parser if the data is really JSON.

Upvotes: 2

matt burns
matt burns

Reputation: 25450

I fully agree with the JSON answers, but if this is a one-off hack, you could just do this:

    String input = "[\"4fd5f684815345\",\"4fd6ef3e60a676854651\",\"4fd83c33c19164512153\"]";
    input = input.replace("[", "");
    input = input.replace("]", "");
    input = input.replace("\"", "");
    String[] parts = input.split(",");

Upvotes: 2

HJW
HJW

Reputation: 23443

It would appear to be that this could be a JSON String. In which case, you may make use of a Java JSON Library to help you parse that into Java native objects.

http://www.json.org/

http://code.google.com/p/google-gson/

String data = "[\"4fd5f684815345\",\"4fd6ef3e60a676854651\",\"4fd83c33c19164512153\"]";

// parse JSON String to JSON Array
JsonArray array = (JsonArray) (new JsonParser()).parse(data);

// build a Java ArrayList
List<String> stringList = new ArrayList<String>();

// for each item in JsonArray, add to Java ArrayList
for (int i = 0; i < array.size(); i++) {
    stringList.add((array.get(i)).getAsString());
}

Upvotes: 4

roberto chiaretti
roberto chiaretti

Reputation: 1

If s is the input string, it can just be as simple as

String[] idArray = s.replaceAll("[\\[\\]\"]", "").split(",");

Upvotes: 0

Pramod Kumar
Pramod Kumar

Reputation: 8014

    String str = "[\"4fd5f684815345\",\"4fd6ef3e60a676854651\",\"4fd83c33c19164512153\"]";
    String strArry[] = null;

    if(str.trim().length() > 0){
        str = str.substring(1 , str.length()-1).replaceAll("\\\"", "");
        strArry = str.split(",");
    }

Upvotes: 1

Related Questions