以下代码来自:http://www.oschina.net/code/explore/jsoup-1.4.1/helper/StringUtil.java
/*** * Join a collection of strings by a seperator * @param strings collection of string objects * @param sep string to place between strings * @return joined string */ public static String join(Collection<String> strings, String sep) { return join(strings.iterator(), sep); } /*** * Join a collection of strings by a seperator * @param strings iterator of string objects * @param sep string to place between strings * @return joined string */ public static String join(Iterator<String> strings, String sep) { if (!strings.hasNext()) return ""; String start = strings.next(); if (!strings.hasNext()) // only one, avoid builder return start; StringBuilder sb = new StringBuilder(64).append(start); while (strings.hasNext()) { sb.append(sep); sb.append(strings.next()); } return sb.toString(); }?
StringUtils.join
提供了九种join方法
org.apache.commons.lang.StringUtils join方法 写道 static String join(Collection collection, char separator)?
将数组中的字符串合并。
数组的定义方式如下:
ARR=(S1 S2 S3)
?
格式1:STR=${ARR[*])
格式2:STR=${ARR[@])
格式3:STR="${ARR[*]}"
格式4:STR="${ARR[@]}"
?
[root@jfht ~]# ARR=(S1 S2 S3)
[root@jfht ~]# echo "${ARR[*]}"
S1 S2 S3
[root@jfht ~]# echo "${ARR[@]}"
???????
S1 S2 S3
[root@jfht ~]#
?
str_join() {
??? local dst
??? for s in "$@"
??? do
??????? dst=${dst}${s}
??? done
??? echo "$dst"
}
?
正确:str_join "$ARR[@]"
错误:str_join $ARR[@]
错误:str_join "$ARR[*]"
错误:str_join $ARR[*]
?
[root@jfht ~]# declare -f str_join
str_join ()
{
??? local dst;
??? for s in "$@";
??? do
??????? dst=${dst}${s};
??? done;
??? echo "$dst"
}
[root@jfht ~]# ARR=(yes no 'hello world')
[root@jfht ~]# str_join ${ARR[*]}
yesnohelloworld
[root@jfht ~]# str_join ${ARR[@]}
yesnohelloworld
[root@jfht ~]# str_join "${ARR[*]}"
yes no hello world
[root@jfht ~]# str_join "${ARR[@]}"
yesnohello world
[root@jfht ~]#
?
实现方式一
str_join_sep ()
{
??? local sep="$1"
??? shift
??? local dst
??? for s in "$@"
??? do
??????? if [ "$dst" ]; then
??????????? dst=${dst}${sep}${s}
??????? else
??????????? dst=${s}
??????? fi
??? done
??? echo "$dst"
}
?
正确:str_join_sep "$SEP" "$ARR[@]"
注意双引号的使用。
?
[root@jfht ~]# declare -f str_join_sep
str_join_sep ()
{
??? local sep="$1";
??? shift;
??? local dst;
??? for s in "$@";
??? do
??????? if [ "$dst" ]; then
??????????? dst=${dst}${sep}${s};
??????? else
??????????? dst=${s};
??????? fi;
??? done;
??? echo "$dst"
}
[root@jfht ~]# ARR=(yes no 'hello world')????
[root@jfht ~]# SEP=,
[root@jfht ~]# str_join_sep "$SEP" "${ARR[@]}"
yes,no,hello world
[root@jfht ~]#
?
实现方式二
str_join_sep ()
{
??? local sep="$1"
??? shift
??? local dst="$1"
??? shift
??? for s in "$@"
??? do
??????? dst=${dst}${sep}${s}
??? done
??? echo "$dst"
}
?
[root@jfht ~]# declare -f str_join_sep
str_join_sep ()
{
??? local sep="$1";
??? shift;
??? local dst="$1";
??? shift;
??? for s in "$@";
??? do
??????? dst=${dst}${sep}${s};
??? done;
??? echo "$dst"
}
[root@jfht ~]# ARR=(yes no 'hello world')
[root@jfht ~]# SEP=,
[root@jfht ~]# str_join_sep "$SEP" "${ARR[@]}"
yes,no,hello world
[root@jfht ~]# SEP=:
[root@jfht ~]# str_join_sep "$SEP" "${ARR[@]}"
yes:no:hello world
[root@jfht ~]# SEP='|'
注意加上单引号,否则Bash认为是管道线。
[root@jfht ~]# str_join_sep "$SEP" "${ARR[@]}"
yes|no|hello world
[root@jfht ~]#
?
?
本文链接:http://codingstandards.iteye.com/blog/1180348 ? (转载请注明出处)
返回目录:Java程序员的Bash实用指南系列之字符串处理(目录)?
上节内容:Bash字符串处理(与Java对照) - 12.字符串连接
下节内容:Bash字符串处理(与Java对照) - 14.判断是否包含另外的字符串(多达6种方法)
?
?