← Back to Perl Course | Chapter 3: Strings | Lesson 6 of 7

split और join

split एक string को pieces में तोड़ता है और join pieces को वापस एक string में जोड़ता है।

In this page:

  1. split और join

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

perl
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
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. "." या "+" जैसे regex special characters वाली एक plain string split को pass करना
  2. यह भूल जाना कि trailing empty fields remove होते हैं
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.