← Back to Bash Course | Chapter 5: Functions | Lesson 3 of 11

Substring निकालना

Substring extraction एक लंबी ribbon से एक slice काटने जैसा है। आप बताते हैं कहां से cutting शुरू करनी है और कितना लंबा piece चाहिए।

In this page:

  1. Substring Extraction
Syntax
bash
${variable:start:length}
${variable:start}

Substring Extraction

${variable:start:length} एक given index (0-based) से एक given number of characters के लिए string का एक हिस्सा extract करता है। length छोड़ने से start से end तक सब कुछ extract होता है।

यह दूसरी languages में मिलने वाले एक substring function का Bash का built-in equivalent है।

Note: एक negative start, जैसे ${var: -3}, string के end से extract करता है (minus से पहले required space पर ध्यान दें)।

उदाहरण: Substring Extraction

bash
#!/bin/bash
text="Hello, Bash World!"
echo "${text:0:5}"
echo "${text:7:4}"
echo "${text:7}"
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. ${str:1:3} उपयोग करना और 3 को end index मानना, जब यह length है।
  2. यह भूल जाना कि offsets 0 से शुरू होते हैं।
  3. बिना space के एक negative offset उपयोग करना, जैसे ${str:-3}, जिसका मतलब है एक default value; ${str: -3} लिखें।

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.