Java中的split和join

   Javascript中的用于字符串和数组之间转换的split和join函数使用起来非常方便,在Java中也有这两个函数,只不过join是在apache commons的lang库里实现的。

 1 import org.apache.commons.lang3.StringUtils;
 2 
 3 public class SplitJoin {
 4     public static void main(String[] args){
 5         String str = "a|b|c|d|e|f|g";
 6         String[] strArray = str.split("[|]");
 7         for(int i=0; i<strArray.length; i++){
 8             System.out.println(strArray[i]);
 9         }
10         System.out.println(StringUtils.join(strArray, "|"));
11     }
12 }

输出结果:

1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 a|b|c|d|e|f|g

 

posted on 2015-10-15 21:54  肥兔子爱豆畜子  阅读(618)  评论(0编辑  收藏  举报

导航