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

strsplit का उपयोग

strsplit text के एक piece को जहाँ भी एक separator appear हो वहाँ parts में काटता है, हर line पर एक sandwich काटने की तरह। इस तरह एक sentence words की एक list बन सकता है।

In this page:

  1. strsplit
Syntax
r
parts <- strsplit(x, split)
parts[[1]]

strsplit

strsplit(x, split) जहाँ भी separator pattern occur हो एक string को pieces में split करता है, एक list return करते हुए (क्योंकि हर input string अलग संख्या में pieces में split हो सकती है)।

पहला result access करने के लिए list में index करना चाहिए, जैसे strsplit(x, ",")[[1]]। यह CSV rows जैसे delimited text parse करने का standard तरीका है।

Note: strsplit() हमेशा एक list return करता है, एक single string input के लिए भी -- pieces का character vector पाने के लिए [[1]] इस्तेमाल करें।

उदाहरण: strsplit

markup
csv_line <- "apple,banana,cherry"

parts <- strsplit(csv_line, ",")[[1]]
print(parts)
cat("Second item:", parts[2], "\n")
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. result को एक vector की तरह treat करना और सीधे parts[2] लिखना, जब strsplit एक list return करता है और आपको पहले [[1]] चाहिए।
  2. "." या "|" जैसे एक special regex character पर split करना, जो unexpectedly match करता है; fixed = TRUE इस्तेमाल करें।
  3. separator पर खत्म होने वाली एक string के अंत में एक empty piece की उम्मीद करना, जब एक trailing empty string drop हो जाती है।
🔒

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.