Java - Split and trim in one shot

后端 未结 8 1012
鱼传尺愫
鱼传尺愫 2020-12-13 23:25

I have a String like this : String attributes = \" foo boo, faa baa, fii bii,\" I want to get a result like this :

String[] result = {\"foo boo\         


        
8条回答
  •  一向
    一向 (楼主)
    2020-12-14 00:01

    create your own custom function

    private static String[] split_and_trim_in_one_shot(String string){
     String[] result  = string.split(",");
     int array_length = result.length;
    
     for(int i =0; i < array_length ; i++){
      result[i]=result[i].trim();
     }
     return result;
    

    Overload with a consideration for custom delimiter

    private static String[] split_and_trim_in_one_shot(String string, String delimiter){
     String[] result  = string.split(delimiter);
     int array_length = result.length;
    
     for(int i =0; i < array_length ; i++){
      result[i]=result[i].trim();
     }
     return result;
    

提交回复
热议问题