help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: (question) fast split/join of strings


From: Lawrence Velázquez
Subject: Re: (question) fast split/join of strings
Date: Tue, 17 Sep 2024 02:56:05 -0400

This question is more appropriate for help-bash than bug-bash.

On Tue, Sep 17, 2024, at 2:21 AM, William Park wrote:
> Hi all,
>
> Is there fast way of splitting and joining of strings in the recent Bash 
> versions?

Define "fast".

> For splitting, I'm aware of
>      old="a,b,c"
>      IFS=, read -a arr <<< "$old"
>
> For joining,
>      new=$(IFS=,; echo "${arr[*]}")
> or
>      new=$(IFS=,; echo "$*")

A joining alternative that avoids subshells:

        printf -v new '%s,' "${arr[@]}"
        new=${new%?}

Another:

        strjoin() {
                local IFS=$1
                shift
                REPLY=$*
        }
        strjoin , "${arr[@]}"
        new=$REPLY

-- 
vq



reply via email to

[Prev in Thread] Current Thread [Next in Thread]