字符串数组与字符串之间的互转(join/split)(转)
原文:http://blog.csdn.net/szwangdf/article/details/4177104
1、Java
1-1、字符串数组=>字符串:StringUtils: join(Object[] array, String separator)
例:
/* *Join Strings using separator >>>AB$#$CD$#$EF */ import org.apache.commons.lang.StringUtils; public class StringUtilsTrial { public static void main(String[] args) { // Join all Strings in the Array into a Single String, // separated by $#$ System.out. println (StringUtils. join ( new String[] { "AB" , "CD" , "EF" }, "$#$" )); } } |
1-2、字符串=>字符串数组:String: split(String separator) 例:
public SplitDemo() { String s = "AB$#$CD$#$EF" ; // 在每个空格字符处进行分解。 String[] ss = s.split( "$#$" ); } |
2、JavaScript
2-1、字符串数组=>字符串:Array: join(String separator)
例:
- <mce:script type="text/javascript">
- <!--
- var a = new Array("a","b","c");
- a.join("|");
- alert(a);
- // -->
- </mce:script>
2-2、字符串=>字符串数组:String: split(String separator)、String: split(String separator, int length)
例:
- <mce:script type="text/javascript">
- <!--
- var str = "一二三四";
- var str1 = "篮球、排球、乒乓球";
- var arr = str.split("");//全部分割
- var arr1 = str1.split("、");//按照顿号分割
- var arr2 = str1.split("、",2);//按照顿号分割,保留两段
- // -->
- </mce:script>