"함수 implode()"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 5개는 보이지 않습니다)
9번째 줄: 9번째 줄:
==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<source lang='bash'>
<syntaxhighlight lang='bash'>
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
ARR=("John Smith" "Jane Doe" "Mike Barnes" "Kevin Patterson")
STR=$(printf ";%s" "${ARR[@]}")
STR=$(printf ";%s" "${ARR[@]}")
15번째 줄: 15번째 줄:
echo $STR
echo $STR
# John Smith;Jane Doe;Mike Barnes;Kevin Patterson
# John Smith;Jane Doe;Mike Barnes;Kevin Patterson
</source>
</syntaxhighlight>


==C++==
==C++==
[[분류: C++]]
[[분류: C++]]
<source lang='cpp'>
<syntaxhighlight lang='cpp'>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
38번째 줄: 38번째 줄:
     // abc_defgh_ijk
     // abc_defgh_ijk
}
}
</source>
</syntaxhighlight>


==C#==
==C#==
[[category: Csharp]]
[[category: Csharp]]
<source lang="csharp">
<syntaxhighlight lang="csharp">
string[] arr = { "a", "b", "c" };
string[] arr = { "a", "b", "c" };
string str = string.Join("-", arr);
string str = string.Join("-", arr);
// a-b-c
// a-b-c
</source>
</syntaxhighlight>
 
==Go==
{{참고|Go Join()}}
[[분류: Go]]
<syntaxhighlight lang='go'>
package main
 
import (
    "fmt"
    "strings"
)
 
func main() {
    fruits := []string{"apple", "banana", "mango"}
    fmt.Println(strings.Join(fruits," ")) // apple banana mango
}
</syntaxhighlight>


==Java==
==Java==
{{참고|자바 String.join()}}
{{참고|자바 String.join()}}
[[category:Java]]
[[category:Java]]
<source lang='java'>
<syntaxhighlight lang='java'>
System.out.println( String.join(" - ", "Alice", "Bob", "Carol") );
System.out.println( String.join(" - ", "Alice", "Bob", "Carol") );
// Alice - Bob - Carol
// Alice - Bob - Carol
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
System.out.println( String.join(" - ", new String[] {"Alice", "Bob", "Carol"}) );
System.out.println( String.join(" - ", new String[] {"Alice", "Bob", "Carol"}) );
// Alice - Bob - Carol
// Alice - Bob - Carol
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
System.out.println( String.join(" - ", Arrays.asList("Alice", "Bob", "Carol")) );
System.out.println( String.join(" - ", Arrays.asList("Alice", "Bob", "Carol")) );
// Alice - Bob - Carol
// Alice - Bob - Carol
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
String[] strs = {"Alice", "Bob", "Carol"};
String[] strs = {"Alice", "Bob", "Carol"};
System.out.println( String.join(" - ", strs) );
System.out.println( String.join(" - ", strs) );
// Alice - Bob - Carol
// Alice - Bob - Carol
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
List<String> strs = Arrays.asList("Alice", "Bob", "Carol");
List<String> strs = Arrays.asList("Alice", "Bob", "Carol");
System.out.println( String.join(" - ", strs) );
System.out.println( String.join(" - ", strs) );
// Alice - Bob - Carol
// Alice - Bob - Carol
</source>
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[category: JavaScript]]
[[category: JavaScript]]
<source lang="JavaScript">
{{참고|JavaScript join()}}
<syntaxhighlight lang="JavaScript">
["a", "b", "c"].join("-")          //  'a-b-c'
["a", "b", "c"].join("-")          //  'a-b-c'
</source>
</syntaxhighlight>


==Kotlin==
==Kotlin==
[[분류: Kotlin]]
[[분류: Kotlin]]
{{참고|Kotlin joinToString()}}
{{참고|Kotlin joinToString()}}
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
fun main(args: Array<String>) {
fun main(args: Array<String>) {
     val fruits = listOf("Apple", "Banana", "Mango", "Orange");
     val fruits = listOf("Apple", "Banana", "Mango", "Orange");
90번째 줄: 108번째 줄:
     // Apple-Banana-Mango-Orange
     // Apple-Banana-Mango-Orange
}
}
</source>
</syntaxhighlight>


==Objective-C==
==Objective-C==
[[category: Objective-C]]
[[category: Objective-C]]
<source lang="objc">
<syntaxhighlight lang="objc">
NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSArray *arr = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
NSString *str = [arr componentsJoinedByString:@"-"];      // 'a-b-c'
NSString *str = [arr componentsJoinedByString:@"-"];      // 'a-b-c'
</source>
</syntaxhighlight>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang="perl">
<syntaxhighlight lang="perl">
join( '-', ('a', 'b', 'c'));      # 'a-b-c'
join( '-', ('a', 'b', 'c'));      # 'a-b-c'
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
{{참고|PHP implode()}}
{{참고|PHP implode()}}
<source lang='php'>
<syntaxhighlight lang='php' run>
$arr = array("a","b","c");
$arr = ["a","b","c"];
$str = implode("-", $arr);
echo implode("-", $arr);
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
{{참고|파이썬 join()}}
{{참고|파이썬 join()}}
<source lang="python">
<syntaxhighlight lang="python" run>
print( "-".join(["a", "b", "c"]) )
print( "-".join(["a", "b", "c"]) )
# a-b-c
</syntaxhighlight>
</source>
<syntaxhighlight lang='Python' run>
<source lang='Python'>
lst = ['Apple', 'Banana', 'Mango', 'Orange']
lst = ['Apple', 'Banana', 'Mango', 'Orange']
print( " + ".join(lst) )
print( " + ".join(lst) )
# Apple + Banana + Mango + Orange
</syntaxhighlight>
</source>
<syntaxhighlight lang='Python' run>
<source lang='Python'>
nums = [1,2,3,4]
nums = [1,2,3,4]
print( " ".join(map(str,nums)) )
print( " ".join(map(str,nums)) )
# 1 2 3 4
</syntaxhighlight>
</source>


==R==
==R==
[[분류: R]]
[[분류: R]]
{{참고|R paste()}}
{{참고|R paste()}}
<source lang='r'>
<syntaxhighlight lang='r'>
paste("ab","cd","ef",sep="-")
paste("ab","cd","ef",sep="-")
## [1] "ab-cd-ef"
## [1] "ab-cd-ef"
</source>
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang="ruby">
<syntaxhighlight lang="ruby">
["a", "b", "c"].join("-")          #  'a-b-c'
["a", "b", "c"].join("-")          #  'a-b-c'
</source>
</syntaxhighlight>


==Scheme==
==Scheme==
[[category: Scheme]]
[[category: Scheme]]
<source lang="scheme">
<syntaxhighlight lang="scheme">
(use-modules (srfi srfi-13))
(use-modules (srfi srfi-13))
(string-join '("a" "b" "c") "-")  ;  "a-b-c"
(string-join '("a" "b" "c") "-")  ;  "a-b-c"
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[함수 explode()]]
*[[함수 explode()]]
*[[함수 str_split()]]
*[[함수 str_split()]]

2021년 10월 5일 (화) 04:00 기준 최신판

  다른 뜻에 대해서는 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 같이 보기[ | ]