Strings और Runes
In this page:
s := "text"
r := 'x' // rune
bytes := []byte(s)
runes := []rune(s)
Strings Are Byte Sequences
एक Go string मूल रूप से bytes का एक immutable sequence है। साधारण ASCII text के लिए हर byte एक character है, लेकिन Unicode text (जैसे emoji या non-Latin scripts) के लिए एक single character को UTF-8 उपयोग करके कई bytes में encode किया जा सकता है।
उदाहरण: Strings Are Byte Sequences
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create s and infer the type
s := "Go"
// Print the values followed by a newline
fmt.Println("bytes:", len(s))
// Print the values followed by a newline
fmt.Println("first byte:", s[0])
}
Login to try C/C++/Java/PHP code in the editor
What Is a Rune?
एक rune एक Unicode code point -- एक असली, इंसान द्वारा महसूस किया जाने वाला character -- represent करता है, UTF-8 में encode होने में चाहे कितने bytes लगें। किसी string को []rune में convert करना आपको एक slice देता है जहां हर element बिल्कुल एक character है।
उदाहरण: What Is a Rune?
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create s and infer the type
s := "café"
// Print the values followed by a newline
fmt.Println("byte length:", len(s))
// Print the values followed by a newline
fmt.Println("rune length:", len([]rune(s)))
}
Login to try C/C++/Java/PHP code in the editor
Iterating Correctly with for range
किसी string पर 'for range' उपयोग करना इसे अपने आप rune by rune decode करता है, आपको हर step पर सही starting byte index और पूरा Unicode code point देते हुए, जो multi-byte characters को अलग करने के बजाय सही से handle करता है।
उदाहरण: Iterating Correctly with for range
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
for i, r := range "café" {
// Print using a format string
fmt.Printf("index %d: %c\n", i, r)
}
}
Login to try C/C++/Java/PHP code in the editor
Strings Are Immutable
बनने के बाद, Go string की contents को कभी जगह पर बदला नहीं जा सकता -- concatenation जैसे operations जो string को modify करते दिखते हैं असल में एक बिल्कुल नई string produce करते हैं। text को efficiently टुकड़ा-टुकड़ा करके बनाने के लिए, Go strings.Builder प्रदान करता है।
उदाहरण: Strings Are Immutable
package main
import "fmt"
func main() {
s := "Hello"
s = s + ", Go!" // creates a new string, doesn't mutate the old one
fmt.Println(s)
}
Login to try C/C++/Java/PHP code in the editor
- s[i] से किसी string को index करना और एक character की उम्मीद करना, जबकि यह असल में एक single byte return करता है, multi-byte Unicode characters पर टूटते हुए।
- characters गिनने के लिए len(s) उपयोग करना जब string में multi-byte UTF-8 characters हों -- len byte count देता है, character count नहीं।
- किसी string को जगह पर modify करने की कोशिश करना, जैसे s[0] = H, जो fail होता है क्योंकि Go में strings immutable हैं।
- Go strings bytes के immutable sequences हैं, आमतौर पर UTF-8 encoded text रखते हुए।
- एक rune एक single Unicode code point (एक असली character) represent करता है, int32 के रूप में stored।
- किसी string पर 'for range' rune by rune iterate करता है, multi-byte characters को सही से handle करते हुए।
- non-ASCII strings के लिए len(s) byte count देता है, character count नहीं।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: