함수 explode()

(Explode에서 넘어옴)
  다른 뜻에 대해서는 리눅스 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"]