当前位置:编程学习 > JAVA >>

Bash字符串处理-13.字符串数组连接

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

In Java
以指定的分隔符将字符串数组连接成一个字符串的源码
以下代码来自:http://www.oschina.net/code/explore/jsoup-1.4.1/helper/StringUtil.java
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;
 &n

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,