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

 
(사용자 2명의 중간 판 18개는 보이지 않습니다)
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++]]
<syntaxhighlight lang='cpp'>
#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
}
</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()}}
[[category:Java]]
[[category:Java]]
<source lang='java'>
<syntaxhighlight lang='java'>
static public String implode(String glue, ArrayList<String> strs) {
System.out.println( String.join(" - ", "Alice", "Bob", "Carol") );
String ret = "";
// Alice - Bob - Carol
int strs_size = strs.size();
</syntaxhighlight>
for(int i=0; i<strs_size; i++) ret += (i == strs_size - 1) ? strs.get(i) : strs.get(i) + glue;
<syntaxhighlight lang='java'>
return ret;
System.out.println( String.join(" - ", new String[] {"Alice", "Bob", "Carol"}) );
}
// Alice - Bob - Carol
</source>
</syntaxhighlight>
<syntaxhighlight lang='java'>
System.out.println( String.join(" - ", Arrays.asList("Alice", "Bob", "Carol")) );
// Alice - Bob - Carol
</syntaxhighlight>
<syntaxhighlight lang='java'>
String[] strs = {"Alice", "Bob", "Carol"};
System.out.println( String.join(" - ", strs) );
// Alice - Bob - Carol
</syntaxhighlight>
<syntaxhighlight lang='java'>
List<String> strs = Arrays.asList("Alice", "Bob", "Carol");
System.out.println( String.join(" - ", strs) );
// Alice - Bob - Carol
</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 joinToString()}}
<syntaxhighlight lang='kotlin'>
fun main(args: Array<String>) {
    val fruits = listOf("Apple", "Banana", "Mango", "Orange");
    var s = fruits.joinToString(separator="-");
    println( s )
    // Apple-Banana-Mango-Orange
}
</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]]
<source lang='php'>
{{참고|PHP implode()}}
$arr = array("a","b","c");
<syntaxhighlight lang='php' run>
$str = implode("-", $arr);
$arr = ["a","b","c"];
</source>
echo implode("-", $arr);
</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>
nums = [1,2,3,4]
print( " ".join(map(str,nums)) )
</syntaxhighlight>
 
==R==
[[분류: R]]
{{참고|R paste()}}
<syntaxhighlight lang='r'>
paste("ab","cd","ef",sep="-")
## [1] "ab-cd-ef"
</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 같이 보기[ | ]