`

Bash字符串处理(与Java对照) - 13.字符串数组连接(以指定分隔符合并)

阅读更多

Bash字符串处理(与Java对照) - 13.字符串数组连接(以指定分隔符合并)

In Java

以指定的分隔符将字符串数组连接成一个字符串的源码

以下代码来自: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)
Joins the elements of the provided Collection into a single String containing the provided elements.
static String join(Collection collection, String separator)
Joins the elements of the provided Collection into a single String containing the provided elements.
static String join(Iterator iterator, char separator)
Joins the elements of the provided Iterator into a single String containing the provided elements.
static String join(Iterator iterator, String separator)
Joins the elements of the provided Iterator into a single String containing the provided elements.
static String join(Object[] array)
Joins the elements of the provided array into a single String containing the provided list of elements.
static String join(Object[] array, char separator)
Joins the elements of the provided array into a single String containing the provided list of elements.
static String join(Object[] array, char separator, int startIndex, int endIndex)
Joins the elements of the provided array into a single String containing the provided list of elements.
static String join(Object[] array, String separator)
Joins the elements of the provided array into a single String containing the provided list of elements.
static String join(Object[] array, String separator, int startIndex, int endIndex)
Joins the elements of the provided array into a single String containing the provided list of elements.
 

 

In Bash

将数组中的字符串合并。

数组的定义方式如下:

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种方法)

 

 

4
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics