split और join
split एक string को pieces में तोड़ता है और join pieces को वापस एक string में जोड़ता है।
In this page:
split और join
split एक pattern, एक string, और एक optional limit लेता है और separators के बीच pieces की एक list return करता है। join एक separator और एक list लेता है और एक string return करता है। एक single space का pattern, ' ' लिखा गया, special है: यह whitespace के किसी भी run पर split करता है और leading whitespace ignore करता है।
Note:
split trailing empty fields remove करता है जब तक आप -1 जैसा एक negative limit pass न करें।
उदाहरण: split and join
use strict;
use warnings;
my @fields = split /,/, "red,green,blue";
print "fields: ", scalar(@fields), " -> @fields\n";
my @words = split ' ', " many spaces here ";
print "words: ", join("|", @words), "\n";
my @limited = split /:/, "a:b:c:d", 2;
print "limited: ", join(" / ", @limited), "\n";
my @trailing = split /,/, "a,b,,,";
print "trailing dropped: ", scalar(@trailing), "\n";
my @kept = split /,/, "a,b,,,", -1;
print "trailing kept: ", scalar(@kept), "\n";
my @chars = split //, "perl";
print "chars: @chars\n";
print "joined: ", join("-", @chars), "\n";
my @dots = split /\./, "192.168.1.10";
print "octets: @dots\n";
# Output:
# fields: 3 -> red green blue
# words: many|spaces|here
# limited: a / b:c:d
# trailing dropped: 2
# trailing kept: 5
# chars: p e r l
# joined: p-e-r-l
# octets: 192 168 1 10
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- "." या "+" जैसे regex special characters वाली एक plain string split को pass करना
- यह भूल जाना कि trailing empty fields remove होते हैं
- pehle list की बजाय separator के बाद join इस्तेमाल करना
चैप्टर सारांश
- split (pattern, string, limit) लेता है
- join (separator, list) लेता है
- split ' ' whitespace के runs पर split करता है
- Default से Trailing empty fields remove होते हैं
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: