Search code examples
javasharedpreferences

How to convert String to array in Java


I have a string String myStringArray = "[491174, 414529, 414557]";. I want output 491174, 414529, 414557 just this part. I tried many methods to convert string to array and many other things. I tried something I'm adding the code below.

My Code

String myStringArray = "[491174, 414529, 414557]";
String newArray = myStringArray.replaceAll(" ", "");
System.out.println("Before Remove : " + newArray);

String remove = newArray.replaceAll("[^a-zA-Z0-9]", ",");
String rm = remove.replaceFirst(",", "");

System.out.println("After Remove : " + rm);

Output

Before Remove : [491174,414529,414557]
After Remove : 491174,414529,414557,

As you can see I have , in the end and I don't know how to remove this ,. Please help me if you can.


Solution

  • rm = rm.substring(0, rm.length() - 1);