split और join
split text के एक piece को एक array में काटता है जहाँ भी एक separator appear हो, एक ribbon काटने की तरह। join इसका उल्टा करता है और pieces को वापस एक text में बाँधता है।
In this page:
Syntax
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
csv_line = "apple,banana,cherry"
parts = csv_line.split(",")
puts parts.inspect
joined = parts.join(" | ")
puts joined
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
- एक separator पर split करना और empty trailing items की उम्मीद करना, जब
"a,b,,".split(",")इन्हें drop करता है। - एक array की बजाय एक string पर
joincall करना, क्योंकिjoinarrays से belong करता है। - यह भूल जाना कि बिना argument के
splitwhitespace पर split करता है और extra spaces remove करता है।
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: