How to convert comma-separated String to List?

前端 未结 24 1971
你的背包
你的背包 2020-11-22 16:58

Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for t

24条回答
  •  囚心锁ツ
    2020-11-22 17:28

    You can do it as follows.

    This removes white space and split by comma where you do not need to worry about white spaces.

        String myString= "A, B, C, D";
    
        //Remove whitespace and split by comma 
        List finalString= Arrays.asList(myString.split("\\s*,\\s*"));
    
        System.out.println(finalString);
    

提交回复
热议问题