3310487509
Goto Top

In Java, how do you convert a String to an array?

String myStringArray = "[491174, 414529, 414557]"; I just want this section of output 491174, 414529, and 414557. I tried a variety of approaches to convert strings to arrays and other stuff. I tried something, and I've included the code below.
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);  
This is the result:
Before Remove : [491174,414529,414557]
After Remove : 491174,414529,414557,
This is the specific code that I have been working on a project and I have encountered an issue.
Please assist me if you are able.

Content-Key: 4812709988

Url: https://administrator.de/contentid/4812709988

Ausgedruckt am: 19.03.2024 um 05:03 Uhr

Mitglied: 4400667902
Lösung 4400667902 30.11.2022, aktualisiert am 01.12.2022 um 01:19:27 Uhr
Goto Top
class test {
   public static void main(String args){
      String myString = "[491174, 414529, 414557]";  
      String arr = StringToArray(myString);
      System.out.println(String.join("\n",arr));  
    }
    public static String StringToArray(String str){
         return str.replaceAll("[\\[\\]\\s]","").split(",");  
    }
}

Test: https://tio.run/##VU9dq4MwDH33V4Q@WXAFh2OMsQd/w@6b9aHXlVFXP2jiQC777S5XN8 ...