함수 implode()

Jmnote (토론 | 기여)님의 2020년 1월 23일 (목) 15:46 판 (→‎PHP)
  다른 뜻에 대해서는 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 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

6 JavaScript

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

7 Kotlin

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

8 Objective-C

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

9 Perl

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

10 PHP

$arr = array("a","b","c");
$str = implode("-", $arr);

11 Python

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

12 R

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

13 Ruby

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

14 Scheme

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

15 같이 보기