함수 explode()

Jmnote (토론 | 기여)님의 2021년 4월 22일 (목) 01:06 판 (→‎Ruby)
  다른 뜻에 대해서는 리눅스 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
STR=$'abc\tdef\tghi'
IFS=$'\t' read -ra ARR <<< "$STR"
for VALUE in "${ARR[@]}"
do
    echo "[$VALUE]"
done

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);
print_r( $pieces );
print_r( preg_split('/,|;/', '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 같이 보기