strings Package का उपयोग
In this page:
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
// 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"))
}
Login to try C/C++/Java/PHP code in the editor
Splitting and Joining
strings.Split किसी string को एक separator के आस-पास substrings की एक slice में तोड़ता है, जबकि strings.Join इसका उल्टा करता है, strings की एक slice को एक दिए separator से जोड़े एक में combine करते हुए।
उदाहरण: Splitting and Joining
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Case Conversion and Trimming
strings.ToUpper और ToLower case को normalize करते हैं, जबकि strings.TrimSpace leading और trailing whitespace हटाता है -- user-provided text compare या display करने से पहले सभी आम steps।
उदाहरण: Case Conversion and Trimming
// 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)))
}
Login to try C/C++/Java/PHP code in the editor
Replacing Substrings
strings.ReplaceAll किसी substring के हर occurrence को दूसरे से बदल देता है, जबकि strings.Replace आपको एक explicit count argument से यह सीमित करने देता है कि कितने occurrences replace हों।
उदाहरण: Replacing Substrings
// 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"))
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि string में न मिले separator पर strings.Split अब भी पूरी original string रखने वाली एक-element slice return करता है।
- strings.Replace को गलत count argument (n) के साथ उपयोग करना, यह न समझते हुए कि 0 कुछ नहीं बदलता और -1 सभी occurrences बदल देता है।
- यह मान लेना कि 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 करते हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: