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

101번째 줄: 101번째 줄:
[[category: Python]]
[[category: Python]]
<source lang="python">
<source lang="python">
(a, b) = "hello,world".split(",")
(a, b) = 'hello,world'.split(',')
print(a)
print(a)
# hello
# hello
113번째 줄: 113번째 줄:
<source lang="python">
<source lang="python">
print( 'abc,defgh,ijk'.split(',') )
print( 'abc,defgh,ijk'.split(',') )
# ["abc", "defgh", "ijk"]
# ['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']
 
</source>
</source>



2014년 8월 24일 (일) 23:31 판

split
explode

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

2 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');

3 Erlang

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

4 Java

"abc,defgh,ijk".split(",");                 // {"abc", "defgh", "ijk"}
"abc,defgh;ijk".split(",|;");               // {"abc", "defgh", "ijk"}

5 JavaScript

var str = 'How are you doing today?';
var arr = str.split(' ');

6 Objective-C

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

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

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

9 PHP

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

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

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

12 같이 보기