← Back to Go Course | Chapter 14: Standard Library & HTTP | Lesson 1 of 10

strings Package का उपयोग

strings package text को काटने, ढूंढने, और फिर से आकार देने के लिए छोटे helpers से भरा एक toolbox है।
Syntax
go
strings.Contains(s, substr)
strings.Split(s, sep)
strings.Join(parts, sep)
strings.Replace(s, old, new, n)

Searching Within Strings

strings.Contains, HasPrefix, और HasSuffix जांचते हैं कि कोई string किसी दिए substring को contain करती है, उससे शुरू होती है, या उस पर खत्म होती है, manual index math के बिना एक साधारण boolean return करते हुए।

उदाहरण: Searching Within Strings

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strings"
)

// main is where the program starts running
func main() {
	// Short declaration: create s and infer the type
	s := "Go is fun"
	// Print the values followed by a newline
	fmt.Println(strings.Contains(s, "fun"))
	// Print the values followed by a newline
	fmt.Println(strings.HasPrefix(s, "Go"))
	fmt.Println(strings.HasSuffix(s, "fun"))
}

Splitting and Joining

strings.Split किसी string को एक separator के आस-पास substrings की एक slice में तोड़ता है, जबकि strings.Join इसका उल्टा करता है, strings की एक slice को एक दिए separator से जोड़े एक में combine करते हुए।

उदाहरण: Splitting and Joining

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strings"
)

// main is where the program starts running
func main() {
	// Short declaration: create parts and infer the type
	parts := strings.Split("apple,banana,cherry", ",")
	// Print the values followed by a newline
	fmt.Println(parts)
	// Short declaration: create joined and infer the type
	joined := strings.Join(parts, " | ")
	// Print the values followed by a newline
	fmt.Println(joined)
}

Case Conversion and Trimming

strings.ToUpper और ToLower case को normalize करते हैं, जबकि strings.TrimSpace leading और trailing whitespace हटाता है -- user-provided text compare या display करने से पहले सभी आम steps।

उदाहरण: Case Conversion and Trimming

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strings"
)

// main is where the program starts running
func main() {
	// Short declaration: create raw and infer the type
	raw := "  Hello Go  "
	// Print the values followed by a newline
	fmt.Println(strings.ToUpper(strings.TrimSpace(raw)))
}

Replacing Substrings

strings.ReplaceAll किसी substring के हर occurrence को दूसरे से बदल देता है, जबकि strings.Replace आपको एक explicit count argument से यह सीमित करने देता है कि कितने occurrences replace हों।

उदाहरण: Replacing Substrings

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"strings"
)

// main is where the program starts running
func main() {
	// Short declaration: create s and infer the type
	s := "one two two three"
	// Print the values followed by a newline
	fmt.Println(strings.ReplaceAll(s, "two", "2"))
}
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. यह भूल जाना कि string में न मिले separator पर strings.Split अब भी पूरी original string रखने वाली एक-element slice return करता है।
  2. strings.Replace को गलत count argument (n) के साथ उपयोग करना, यह न समझते हुए कि 0 कुछ नहीं बदलता और -1 सभी occurrences बदल देता है।
  3. यह मान लेना कि strings.Contains case-insensitive है -- यह एक सख्त substring match है; case-insensitive comparisons के लिए strings.EqualFold या दोनों sides को lowercase करें।
चैप्टर सारांश
  • strings.Contains, strings.HasPrefix, और strings.HasSuffix विभिन्न positions पर substrings जांचते हैं।
  • strings.Split किसी string को एक separator का उपयोग करके एक slice में तोड़ता है; strings.Join उल्टा करता है।
  • strings.ToUpper/ToLower/TrimSpace comparison या display के लिए text normalize करते हैं।
  • strings.Replace और strings.ReplaceAll किसी substring के occurrences substitute करते हैं।

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.