String में Replacement
String replacement एक story पर find-and-replace उपयोग करने जैसा है: पहला matching word swap करें, या double-slash way उपयोग करने पर उनमें से हर एक को swap करें।
In this page:
Syntax
${variable/pattern/replacement} # first match
${variable//pattern/replacement} # all matches
String Replacement
${variable/pattern/replacement} string में pattern का पहला match replace करता है, जबकि ${variable//pattern/replacement} हर match replace करता है।
यह simple substitutions के लिए sed से pipe करने का एक built-in alternative है। Patterns basic globbing support करते हैं, पूरी तरह regular expressions नहीं।
Note:
Double-slash form उपयोग करें,
${var//old/new}, जब आपको सभी occurrences replace करनी हों, सिर्फ पहली नहीं।उदाहरण: String Replacement
#!/bin/bash
text="foo bar foo baz"
echo "${text/foo/qux}"
echo "${text//foo/qux}"
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. #}
आम गलतियां
${str/a/b}उपयोग करना और सभी occurrences replace होने की उम्मीद करना, जब सिर्फ पहली होती है;${str//a/b}उपयोग करें।- यह उम्मीद करना कि original variable change हो जाएगा, जब यह सिर्फ एक नई value produce करता है।
- Pattern में special characters escape करना भूल जाना, जैसे
/।
🔒
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: