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

substr का उपयोग

substr दो positions के बीच text के एक piece से एक slice काटता है, एक ribbon पर scissors इस्तेमाल करने की तरह। आप बताते हैं कहाँ से शुरू करें और कहाँ रुकें।

In this page:

  1. substr
Syntax
r
substr(x, start, stop)

substr

substr(x, start, stop) दी गई character positions के बीच एक substring extract करता है, दोनों ends inclusive, 1-based indexing इस्तेमाल करते हुए। substr() एक assignment के left side पर भी इस्तेमाल हो सकता है string के हिस्से को in place replace करने के लिए।

यह position से strings slice करने का R का built-in तरीका है।

Note: कई languages के उलट, substr() का stop index inclusive है -- substr("hello", 1, 3) "hel" देता है, position 3 समेत।

उदाहरण: substr

markup
text <- "Hello, R World!"

cat(substr(text, 1, 5), "\n")
cat(substr(text, 8, 8), "\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. start position की तरह 0 इस्तेमाल करना, जब R strings position 1 से शुरू होती हैं।
  2. substr(text, 1, 5) को start और length से confuse करना, जब third argument end position है।
  3. text[1] <- "J" जैसे index से एक string बदलने की कोशिश करना, जो पूरी string replace करता है; substr(text, 1, 1) <- "J" इस्तेमाल करें।
🔒

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.