漂流的老妖怪

导航

 

 一、将逗号分隔的字符串转换成List:

1. 使用JDK的Arrays类:

import java.util.Arrays;
import java.util.List;

public class TestList {
    public static void main(String[] args) {
        String str = "a,b,c,d";
        List<String> list = Arrays.asList(str.split(","));
        
        System.out.println(str);
        System.out.println(list);
    }
}

测试结果:


 2. 使用Guava的Splitter:

import java.util.List;
import com.google.common.base.Splitter;

public class TestList {
    public static void main(String[] args) {
        String str = "a,b,c,d";
        List<String> list = Splitter.on(",").trimResults().splitToList(str); 
        
        System.out.println(str);
        System.out.println(list);
    }
}

测试结果:

 


 3. 使用Apache Commons的StringUtils:

import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.StringUtils;

public class TestList {
    public static void main(String[] args) {
        String str = "a,b,c,d";
        List<String> list = Arrays.asList(StringUtils.split(str,",")); 
        
        System.out.println(str);
        System.out.println(list);
    }
}

测试结果:


 4. 使用Spring Framework的StringUtils:

import java.util.Arrays;
import java.util.List;
import org.springframework.util.StringUtils;

public class TestList {
    public static void main(String[] args) {
        String str = "a,b,c,d";
        List<String> list = Arrays.asList(StringUtils.commaDelimitedListToStringArray(str));
        
        System.out.println(str);
        System.out.println(list);
    }
}

测试结果:


 

二、将List转换为逗号分隔的字符串:

 1. 使用JDK:

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;

public class TestList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();  
        list.add("a");  
        list.add("b");  
        list.add("c");  
        list.add("d");  
        
        String str = "";
        for(String s : list){
            if(StringUtils.isBlank(str)){
                str = s;
            }else {
                str = str + "," + s;
            }
        }
        
        System.out.println(list);
        System.out.println(str);
    }
}

测试结果:

 


 2. 使用Guava的Joiner:

import java.util.ArrayList;
import java.util.List;
import com.google.common.base.Joiner;

public class TestList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();  
        list.add("a");  
        list.add("b");  
        list.add("c");  
        list.add("d");  
        
        String str = Joiner.on(",").join(list);  
        
        System.out.println(list);
        System.out.println(str);
    }
}

测试结果:


 3. 使用Apache Commons的StringUtils:

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;

public class TestList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();  
        list.add("a");  
        list.add("b");  
        list.add("c");  
        list.add("d");  
        
        String str = StringUtils.join(list.toArray(), ",");
        
        System.out.println(list);
        System.out.println(str);
    }
}

测试结果:

 


 4. 使用Spring Framework的StringUtils:

import java.util.ArrayList;
import java.util.List;
import org.springframework.util.StringUtils;

public class TestList {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();  
        list.add("a");  
        list.add("b");  
        list.add("c");  
        list.add("d");  
        
        String str = StringUtils.collectionToDelimitedString(list, ",");
        
        System.out.println(list);
        System.out.println(str);
    }
}

测试结果:

 

 

posted on 2018-07-23 19:36  漂流的老妖怪  阅读(1479)  评论(0编辑  收藏  举报