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

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
8번째 줄: 8번째 줄:
==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<source lang='bash'>
<syntaxhighlight lang='bash'>
STR="John Smith;Jane Doe;Mike Barnes;Kevin Patterson"
STR="John Smith;Jane Doe;Mike Barnes;Kevin Patterson"
IFS=';' read -ra ARR <<< "$STR"
IFS=';' read -ra ARR <<< "$STR"
19번째 줄: 19번째 줄:
# Mike Barnes
# Mike Barnes
# Kevin Patterson
# Kevin Patterson
</source>
</syntaxhighlight>
<source lang='bash'>
<syntaxhighlight lang='bash'>
STR=$'abc\tdef\tghi'
STR=$'abc\tdef\tghi'
IFS=$'\t' read -ra ARR <<< "$STR"
IFS=$'\t' read -ra ARR <<< "$STR"
30번째 줄: 30번째 줄:
# [def]
# [def]
# [ghi]
# [ghi]
</source>
</syntaxhighlight>


==C++==
==C++==
<source lang='cpp'>
<syntaxhighlight lang='cpp'>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
61번째 줄: 61번째 줄:
     // [abc][defgh][ijk][][]
     // [abc][defgh][ijk][][]
}
}
</source>
</syntaxhighlight>


==C#==
==C#==
[[category: Csharp]]
[[category: Csharp]]
<source lang="csharp">
<syntaxhighlight lang="csharp">
"abc,defgh,ijk".Split(',');                // {"abc", "defgh", "ijk"}
"abc,defgh,ijk".Split(',');                // {"abc", "defgh", "ijk"}
"abc,defgh;ijk".Split(',', ';');            // {"abc", "defgh", "ijk"}
"abc,defgh;ijk".Split(',', ';');            // {"abc", "defgh", "ijk"}
71번째 줄: 71번째 줄:
string str = "Hello, eqcode.\rThis is a test message.";
string str = "Hello, eqcode.\rThis is a test message.";
string[] lines = str.Split('\r');
string[] lines = str.Split('\r');
</source>
</syntaxhighlight>


==Erlang==
==Erlang==
[[category: Erlang]]
[[category: Erlang]]
<source lang="php">
<syntaxhighlight lang="php">
string:tokens("abc;defgh;ijk", ";").        %  ["abc", "defgh", "ijk"]
string:tokens("abc;defgh;ijk", ";").        %  ["abc", "defgh", "ijk"]
</source>
</syntaxhighlight>


==Go==
==Go==
[[분류: Go]]
[[분류: Go]]
{{참고|Go Split()}}
{{참고|Go Split()}}
<source lang='go'>
<syntaxhighlight lang='go'>
package main
package main


95번째 줄: 95번째 줄:
fmt.Println( pieces ) // [piece1 piece2 piece3 piece4 piece5 piece6]
fmt.Println( pieces ) // [piece1 piece2 piece3 piece4 piece5 piece6]
}
}
</source>
</syntaxhighlight>


==Java==
==Java==
{{참고|자바 String.split()}}
{{참고|자바 String.split()}}
[[category: Java]]
[[category: Java]]
<source lang="java">
<syntaxhighlight lang="java">
"abc,defgh,ijk".split(",");                // {"abc", "defgh", "ijk"}
"abc,defgh,ijk".split(",");                // {"abc", "defgh", "ijk"}
"abc,defgh;ijk".split(",|;");              // {"abc", "defgh", "ijk"}
"abc,defgh;ijk".split(",|;");              // {"abc", "defgh", "ijk"}
</source>
</syntaxhighlight>
<source lang='java'>
<syntaxhighlight lang='java'>
String str = "abc,defgh,ijk";
String str = "abc,defgh,ijk";
String[] parts = str.split(",");
String[] parts = str.split(",");
113번째 줄: 113번째 줄:
// defgh
// defgh
// ijk
// ijk
</source>
</syntaxhighlight>


==JavaScript==
==JavaScript==
{{참고|JavaScript split()}}
{{참고|JavaScript split()}}
[[category: JavaScript]]
[[category: JavaScript]]
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
var str = 'How are you doing today?';
var str = 'How are you doing today?';
console.log( str.split(' ') );
console.log( str.split(' ') );
// ["How", "are", "you", "doing", "today?"]
// ["How", "are", "you", "doing", "today?"]
</source>
</syntaxhighlight>
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
var str = 'How are you doing today?';
var str = 'How are you doing today?';
console.log( str.split(/ /) );
console.log( str.split(/ /) );
// ["How", "are", "you", "doing", "today?"]
// ["How", "are", "you", "doing", "today?"]
</source>
</syntaxhighlight>
<source lang='javascript'>
<syntaxhighlight lang='javascript'>
var str = 'How are you doing today?';
var str = 'How are you doing today?';
console.log( str.split(/( )/) );
console.log( str.split(/( )/) );
// ["How", " ", "are", " ", "you", " ", "doing", " ", "today?"]
// ["How", " ", "are", " ", "you", " ", "doing", " ", "today?"]
</source>
</syntaxhighlight>


==Kotlin==
==Kotlin==
[[분류: Kotlin]]
[[분류: Kotlin]]
{{참고|Kotlin split()}}
{{참고|Kotlin split()}}
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
fun main(args: Array<String>) {
fun main(args: Array<String>) {
     val s = "John Smith;Jane Doe;Mike Barnes;Kevin Patterson"
     val s = "John Smith;Jane Doe;Mike Barnes;Kevin Patterson"
143번째 줄: 143번째 줄:
     // [John Smith, Jane Doe, Mike Barnes, Kevin Patterson]
     // [John Smith, Jane Doe, Mike Barnes, Kevin Patterson]
}
}
</source>
</syntaxhighlight>
<source lang='kotlin'>
<syntaxhighlight lang='kotlin'>
fun main(args: Array<String>) {
fun main(args: Array<String>) {
     val s = "How are you doing today?"
     val s = "How are you doing today?"
150번째 줄: 150번째 줄:
     // [How, are, you, doing, today?]
     // [How, are, you, doing, today?]
}
}
</source>
</syntaxhighlight>


==Objective-C==
==Objective-C==
[[category: Objective-C]]
[[category: Objective-C]]
<source lang="objc">
<syntaxhighlight lang="objc">
[@"abc,defgh,ijk" componentsSeparatedByString:@","];    // [@"abc", @"defgh", @"ijk"]
[@"abc,defgh,ijk" componentsSeparatedByString:@","];    // [@"abc", @"defgh", @"ijk"]
</source>
</syntaxhighlight>


==Pascal==
==Pascal==
[[category: Pascal]]
[[category: Pascal]]
<source lang="pascal">
<syntaxhighlight lang="pascal">
var
var
   lStrings: TStringList;
   lStrings: TStringList;
172번째 줄: 172번째 줄:
   lStr := lStrings.Strings[2]; // 'ijk'
   lStr := lStrings.Strings[2]; // 'ijk'
end;
end;
</source>
</syntaxhighlight>


==Perl==
==Perl==
[[category: Perl]]
[[category: Perl]]
<source lang="perl">
<syntaxhighlight lang="perl">
split(/spam/, 'Spam eggs spam spam and ham'); # ('Spam eggs ', ' ', ' and ham')
split(/spam/, 'Spam eggs spam spam and ham'); # ('Spam eggs ', ' ', ' and ham')
split(/X/, 'Spam eggs spam spam and ham');    #  ('Spam eggs spam spam and ham')
split(/X/, 'Spam eggs spam spam and ham');    #  ('Spam eggs spam spam and ham')
split(/,|;/, 'abc,defgh;ijk');                # ("abc", "defgh", "ijk")
split(/,|;/, 'abc,defgh;ijk');                # ("abc", "defgh", "ijk")
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[category: PHP]]
[[category: PHP]]
<source lang='php'>
<syntaxhighlight lang='php'>
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(' ', $pizza);
$pieces = explode(' ', $pizza);
</source>
</syntaxhighlight>
<source lang='php'>
<syntaxhighlight lang='php'>
preg_split('/,|;/', 'abc,defgh;ijk');
preg_split('/,|;/', 'abc,defgh;ijk');
// array("abc", "defgh", "ijk")
// array("abc", "defgh", "ijk")
</source>
</syntaxhighlight>


==Python==
==Python==
[[category: Python]]
[[category: Python]]
{{참고|파이썬 split()}}
{{참고|파이썬 split()}}
<source lang="python">
<syntaxhighlight lang="python">
(a, b) = 'hello,world'.split(',')
(a, b) = 'hello,world'.split(',')
print(a)
print(a)
202번째 줄: 202번째 줄:
print(b)
print(b)
# world
# world
</source>
</syntaxhighlight>
<source lang="python">
<syntaxhighlight lang="python">
print( 'ab|cd|ef|gh|ij'.split('|',2) )
print( 'ab|cd|ef|gh|ij'.split('|',2) )
# ['ab', 'cd', 'ef|gh|ij']
# ['ab', 'cd', 'ef|gh|ij']
</source>
</syntaxhighlight>
<source lang="python">
<syntaxhighlight lang="python">
print( 'abc,defgh,ijk'.split(',') )
print( 'abc,defgh,ijk'.split(',') )
# ['abc', 'defgh', 'ijk']
# ['abc', 'defgh', 'ijk']
215번째 줄: 215번째 줄:
# ['abc', 'defgh', 'ijk']
# ['abc', 'defgh', 'ijk']


</source>
</syntaxhighlight>


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang="ruby">
<syntaxhighlight lang="ruby">
print "abc,defgh,ijk".split(",")
print "abc,defgh,ijk".split(",")
# ["abc", "defgh", "ijk"]
# ["abc", "defgh", "ijk"]
228번째 줄: 228번째 줄:
print "hello my friend".split
print "hello my friend".split
# ["hello", "my", "friend"]
# ["hello", "my", "friend"]
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2020년 11월 2일 (월) 02:32 판

  다른 뜻에 대해서는 리눅스 split 문서를 참조하십시오.

1 개요

split
explode

2 Bash

STR="John Smith;Jane Doe;Mike Barnes;Kevin Patterson"
IFS=';' read -ra ARR <<< "$STR"
for VALUE in "${ARR[@]}"
do
    echo $VALUE
done
# John Smith
# Jane Doe
# Mike Barnes
# Kevin Patterson
STR=$'abc\tdef\tghi'
IFS=$'\t' read -ra ARR <<< "$STR"
for VALUE in "${ARR[@]}"
do
    echo "[$VALUE]"
done
# [abc]
# [def]
# [ghi]

3 C++

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
vector<string> explode(char delim, string const & s) {
   vector<string> tokens;
   istringstream tokenStream(s);
   for(string token; getline(tokenStream, token, delim); ) tokens.push_back(token);
   if(s.back()==delim) tokens.push_back("");
   return tokens;
}

int main() {
    vector<string> v1 = explode(',', "abc,defgh,ijk");
    for(string s: v1) cout << "[" << s << "]";
    // [abc][defgh][ijk]
    cout << endl;
    
    vector<string> v2 = explode(',', "abc,defgh,ijk,");
    for(string s: v2) cout << "[" << s << "]";
    // [abc][defgh][ijk][]
    cout << endl;
    
    vector<string> v3 = explode(',', "abc,defgh,ijk,,");
    for(string s: v3) cout << "[" << s << "]";
    // [abc][defgh][ijk][][]
}

4 C#

"abc,defgh,ijk".Split(',');                 // {"abc", "defgh", "ijk"}
"abc,defgh;ijk".Split(',', ';');            // {"abc", "defgh", "ijk"}

string str = "Hello, eqcode.\rThis is a test message.";
string[] lines = str.Split('\r');

5 Erlang

string:tokens("abc;defgh;ijk", ";").        %  ["abc", "defgh", "ijk"]

6 Go

package main

import (
	"fmt"
	"strings"
)

func main() {
	pizza := "piece1 piece2 piece3 piece4 piece5 piece6"
	pieces := strings.Split(pizza, " ")
	fmt.Println( pieces ) // [piece1 piece2 piece3 piece4 piece5 piece6]
}

7 Java

"abc,defgh,ijk".split(",");                 // {"abc", "defgh", "ijk"}
"abc,defgh;ijk".split(",|;");               // {"abc", "defgh", "ijk"}
String str = "abc,defgh,ijk";
String[] parts = str.split(",");
System.out.println(parts[0]);
System.out.println(parts[1]);
System.out.println(parts[2]);
// abc
// defgh
// ijk

8 JavaScript

var str = 'How are you doing today?';
console.log( str.split(' ') );
// ["How", "are", "you", "doing", "today?"]
var str = 'How are you doing today?';
console.log( str.split(/ /) );
// ["How", "are", "you", "doing", "today?"]
var str = 'How are you doing today?';
console.log( str.split(/( )/) );
// ["How", " ", "are", " ", "you", " ", "doing", " ", "today?"]

9 Kotlin

fun main(args: Array<String>) {
    val s = "John Smith;Jane Doe;Mike Barnes;Kevin Patterson"
    println(s.split(";"))
    // [John Smith, Jane Doe, Mike Barnes, Kevin Patterson]
}
fun main(args: Array<String>) {
    val s = "How are you doing today?"
    println(s.split(' '))
    // [How, are, you, doing, today?]
}

10 Objective-C

[@"abc,defgh,ijk" componentsSeparatedByString:@","];    // [@"abc", @"defgh", @"ijk"]

11 Pascal

var
  lStrings: TStringList;
  lStr: string;
begin
  lStrings := TStringList.Create;
  lStrings.Delimiter := ',';
  lStrings.DelimitedText := 'abc,defgh,ijk';
  lStr := lStrings.Strings[0]; // 'abc'
  lStr := lStrings.Strings[1]; // 'defgh'
  lStr := lStrings.Strings[2]; // 'ijk'
end;

12 Perl

split(/spam/, 'Spam eggs spam spam and ham'); # ('Spam eggs ', ' ', ' and ham')
split(/X/, 'Spam eggs spam spam and ham');    #  ('Spam eggs spam spam and ham')
split(/,|;/, 'abc,defgh;ijk');                # ("abc", "defgh", "ijk")

13 PHP

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(' ', $pizza);
preg_split('/,|;/', 'abc,defgh;ijk');
// array("abc", "defgh", "ijk")

14 Python

(a, b) = 'hello,world'.split(',')
print(a)
# hello
print(b)
# world
print( 'ab|cd|ef|gh|ij'.split('|',2) )
# ['ab', 'cd', 'ef|gh|ij']
print( 'abc,defgh,ijk'.split(',') )
# ['abc', 'defgh', 'ijk']
 
import re
print( re.split(",|;", "abc,defgh;ijk") )
# ['abc', 'defgh', 'ijk']

15 Ruby

print "abc,defgh,ijk".split(",")
# ["abc", "defgh", "ijk"]

print "abc,defgh;ijk".split(/,|;/)
# ["abc", "defgh", "ijk"]

print "hello my friend".split
# ["hello", "my", "friend"]

16 같이 보기