0

I have this string : "[ name = preload cacheHits = 0 onDiskHits = 0 inMemoryHits = 0 misses = 0 size = 0 averageGetTime = 0.0 evictionCount = 0 ]",

and want to convert it in array like [ name = preload cacheHits = 0 onDiskHits = 0 inMemoryHits = 0 misses = 0 size = 0 averageGetTime = 0.0 evictionCount = 0 ],

How can we do it?

4
  • 2
    ehm ... I don't see a valid array type there. a composite object, sure, but not an array, since they're different types. Commented Jun 19, 2020 at 10:53
  • It's not array. maybe a MAP is right for you? I see it's more suitable for an associative array Commented Jun 19, 2020 at 11:12
  • @skr have you debug and checked whether it's coming as array or string. if you will debug at response then for array you will get response=ArrayList<E> and for string it will give you response="[ name = preload cacheHits = 0 onDiskHits = 0 inMemoryHits = 0 misses = 0 size = 0 averageGetTime = 0.0 evictionCount = 0 ]" Commented Jun 19, 2020 at 11:52
  • @TanmayNaik, I get the data in string form, I can not do much at the backend as well as the library that sends this info is sending it in string form. I have managed to convert it in Json using javascript. I was looking for a solution in Java. Commented Jun 19, 2020 at 12:05

1 Answer 1

1
String response="[ name = preload cacheHits = 0 onDiskHits = 0 inMemoryHits = 0 misses = 0 size = 0 averageGetTime = 0.0 evictionCount = 0 ]";
String replace = response.replaceAll("^\\[|]$","");
System.out.println(Arrays.asList(replace));
Sign up to request clarification or add additional context in comments.

1 Comment

I never thought I could easily do it using regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.