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

 
(사용자 2명의 중간 판 45개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[category: Array]]
[[category: Array]]
[[category: String]]
[[category: String]]
{{다른뜻|리눅스 split}}
==개요==
;explode
;split
;split
;explode
 
==같이 보기==
{{z컬럼3|
* [[함수 preg_split()]]
*[[함수 explode_with_whitespaces()]]
*[[함수 implode()]]
*[[함수 sscanf()]]
*[[string to array]]
*[[구분자]]
}}


==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<source lang='bash'>
<syntaxhighlight lang='bash' run>
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"
13번째 줄: 25번째 줄:
     echo $VALUE
     echo $VALUE
done
done
# John Smith
</syntaxhighlight>
# Jane Doe
<syntaxhighlight lang='bash' run>
# Mike Barnes
# Kevin Patterson
</source>
<source lang='bash'>
STR=$'abc\tdef\tghi'
STR=$'abc\tdef\tghi'
IFS=$'\t' read -ra ARR <<< "$STR"
IFS=$'\t' read -ra ARR <<< "$STR"
25번째 줄: 33번째 줄:
     echo "[$VALUE]"
     echo "[$VALUE]"
done
done
# [abc]
</syntaxhighlight>
# [def]
 
# [ghi]
==C++==
</source>
{{참고|C++ 문자열 split() 구현}}
<syntaxhighlight lang='cpp' run>
#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][][]
}
</syntaxhighlight>


==C#==
==C#==
[[category: Csharp]]
[[category: Csharp]]
<source lang="csharp">
{{참고|C샵 Split()}}
"abc,defgh,ijk".Split(',');                 // {"abc", "defgh", "ijk"}
<syntaxhighlight lang="csharp" run>
"abc,defgh;ijk".Split(',', ';');           // {"abc", "defgh", "ijk"}
using System;
class Program {
    static void Main() {
        string[] arr;
        arr = "abc,defgh,ijk".Split(',');
        Console.WriteLine( string.Join(" ", arr)); // abc defgh ijk
        arr = "abc,defgh;ijk".Split(',', ';');
        Console.WriteLine( string.Join(" ", arr)); // abc defgh ijk


string str = "Hello, eqcode.\rThis is a test message.";
        string str = "Hello, World!\rThis is a test message.";
string[] lines = str.Split('\r');
        string[] lines = str.Split('\r');
</source>
        Console.WriteLine( string.Join("||", lines) );
    }
}
</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 Split()}}
<syntaxhighlight lang='go' run>
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]
}
</syntaxhighlight>


==Java==
==Java==
{{참고|자바 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>
<syntaxhighlight lang='java'>
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
</syntaxhighlight>


==JavaScript==
==JavaScript==
{{참고|JavaScript split()}}
[[category: JavaScript]]
[[category: JavaScript]]
<source lang='javascript'>
<syntaxhighlight lang='javascript' run>
var str = 'How are you doing today?';
var str = 'How are you doing today?';
var arr = str.split(' ');
console.log( str.split(' ') ); // ["How", "are", "you", "doing", "today?"]
</source>
</syntaxhighlight>
<syntaxhighlight lang='javascript' run>
var str = 'How are you doing today?';
console.log( str.split(/ /) ); // ["How", "are", "you", "doing", "today?"]
</syntaxhighlight>
<syntaxhighlight lang='javascript' run>
var str = 'How are you doing today?';
console.log( str.split(/( )/) ); // ["How", " ", "are", " ", "you", " ", "doing", " ", "today?"]
</syntaxhighlight>
 
==Kotlin==
[[분류: Kotlin]]
{{참고|Kotlin split()}}
<syntaxhighlight lang='kotlin' run>
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]
}
</syntaxhighlight>
<syntaxhighlight lang='kotlin' run>
fun main(args: Array<String>) {
    val s = "How are you doing today?"
    println(s.split(' ')) // [How, are, you, doing, today?]
}
</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;
80번째 줄: 176번째 줄:
   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" run>
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'>
{{참고|PHP explode()}}
<syntaxhighlight lang='php' run>
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
$pieces = explode(' ', $pizza);
</source>
print_r( $pieces );
<source lang='php'>
</syntaxhighlight>
preg_split('/,|;/', 'abc,defgh;ijk');
<syntaxhighlight lang='php' run>
// array("abc", "defgh", "ijk")
print_r( preg_split('/,|;/', 'abc,defgh;ijk') );
</source>
</syntaxhighlight>


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


==Ruby==
==Ruby==
[[category: Ruby]]
[[category: Ruby]]
<source lang="ruby">
<syntaxhighlight lang="ruby" run>
print "abc,defgh,ijk".split(",")
print "abc,defgh,ijk".split(",")   # ["abc", "defgh", "ijk"]
# ["abc", "defgh", "ijk"]
print "abc,defgh;ijk".split(/,|;/) # ["abc", "defgh", "ijk"]
 
print "hello my friend".split     # ["hello", "my", "friend"]
print "abc,defgh;ijk".split(/,|;/)
</syntaxhighlight>
# ["abc", "defgh", "ijk"]
 
print "hello my friend".split
# ["hello", "my", "friend"]
</source>
 
==같이 보기==
*[[implode]]
*[[구분자]]

2023년 9월 29일 (금) 15:22 기준 최신판

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

1 개요[ | ]

explode
split

2 같이 보기[ | ]

3 Bash[ | ]

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

4 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][][]
}

5 C#[ | ]

using System;
class Program {
    static void Main() {
        string[] arr;
        arr = "abc,defgh,ijk".Split(',');
        Console.WriteLine( string.Join(" ", arr)); // abc defgh ijk
        arr = "abc,defgh;ijk".Split(',', ';');
        Console.WriteLine( string.Join(" ", arr)); // abc defgh ijk

        string str = "Hello, World!\rThis is a test message.";
        string[] lines = str.Split('\r');
        Console.WriteLine( string.Join("||", lines) );
    }
}

6 Erlang[ | ]

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

7 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]
}

8 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

9 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?"]

10 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?]
}

11 Objective-C[ | ]

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

12 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;

13 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")

14 PHP[ | ]

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

15 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']

16 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"]