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

split और join

split text के एक piece को एक array में काटता है जहाँ भी एक separator appear हो, एक ribbon काटने की तरह। join इसका उल्टा करता है और pieces को वापस एक text में बाँधता है।

In this page:

  1. split और join
Syntax
ruby
parts = string.split(separator)
joined = array.join(separator)

split और join

String#split(separator) जहाँ भी separator occur हो एक string को substrings के एक array में तोड़ता है, और Array#join(separator) इसका उल्टा करता है, array elements को वापस एक single string में combine करते हुए।

ये आमतौर पर CSV data जैसे delimited text parse और reconstruct करते वक्त साथ pair होते हैं। बिना arguments के split call करना किसी भी whitespace पर split करता है।

Note: बिना argument के text.split whitespace के किसी भी run पर split करता है, जो plain sentences tokenize करने के लिए handy है।

उदाहरण: split and join

markup
csv_line = "apple,banana,cherry"

parts = csv_line.split(",")
puts parts.inspect

joined = parts.join(" | ")
puts joined
Related Topics
{# 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. एक separator पर split करना और empty trailing items की उम्मीद करना, जब "a,b,,".split(",") इन्हें drop करता है।
  2. एक array की बजाय एक string पर join call करना, क्योंकि join arrays से belong करता है।
  3. यह भूल जाना कि बिना argument के split whitespace पर split करता है और extra spaces 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.