함수 implode()

  다른 뜻에 대해서는 Join 문서를 참조하십시오.

1 개요[ | ]

implode
join

2 Bash[ | ]

ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
STR=$(printf ";%s" "${ARR[@]}")
STR=${STR:1}
echo $STR
# John Smith;Jane Doe;Mike Barnes;Kevin Patterson

3 C++[ | ]

#include <iostream>
#include <vector>
using namespace std;
string implode( const string &glue, const vector<string> &pieces ) {
    string res;
    int len = pieces.size();
    for(int i=0; i<len; i++) {
        res += pieces[i];
        if( i < len-1 ) res += glue;
    }
    return res;
}
int main() {
    vector<string> v = {"abc","defgh","ijk"};
    string s = implode("_", v);
    cout << s << endl;
    // abc_defgh_ijk
}

4 C#[ | ]

string[] arr = { "a", "b", "c" };
string str = string.Join("-", arr);
// a-b-c

5 Go[ | ]

package main

import (
    "fmt"
    "strings"
)

func main() {
    fruits := []string{"apple", "banana", "mango"}
    fmt.Println(strings.Join(fruits," ")) // apple banana mango
}

6 Java[ | ]

System.out.println( String.join(" - ", "Alice", "Bob", "Carol") );
// Alice - Bob - Carol
System.out.println( String.join(" - ", new String[] {"Alice", "Bob", "Carol"}) );
// Alice - Bob - Carol
System.out.println( String.join(" - ", Arrays.asList("Alice", "Bob", "Carol")) );
// Alice - Bob - Carol
String[] strs = {"Alice", "Bob", "Carol"};
System.out.println( String.join(" - ", strs) );
// Alice - Bob - Carol
List<String> strs = Arrays.asList("Alice", "Bob", "Carol");
System.out.println( String.join(" - ", strs) );
// Alice - Bob - Carol

7 JavaScript[ | ]

["a", "b", "c"].join("-")          //  'a-b-c'

8 Kotlin[ | ]

fun main(args: Array<String>) {
    val fruits = listOf("Apple", "Banana", "Mango", "Orange");
    var s = fruits.joinToString(separator="-");
    println( s )
    // Apple-Banana-Mango-Orange
}

9 Objective-C[ | ]

NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSString *str = [arr componentsJoinedByString:@"-"];       // 'a-b-c'

10 Perl[ | ]

join( '-', ('a', 'b', 'c'));       # 'a-b-c'

11 PHP[ | ]

$arr = ["a","b","c"];
echo implode("-", $arr);

12 Python[ | ]

print( "-".join(["a", "b", "c"]) )
lst = ['Apple', 'Banana', 'Mango', 'Orange']
print( " + ".join(lst) )
nums = [1,2,3,4]
print( " ".join(map(str,nums)) )

13 R[ | ]

paste("ab","cd","ef",sep="-")
## [1] "ab-cd-ef"

14 Ruby[ | ]

["a", "b", "c"].join("-")          #  'a-b-c'

15 Scheme[ | ]

(use-modules (srfi srfi-13))
(string-join '("a" "b" "c") "-")   ;  "a-b-c"

16 같이 보기[ | ]