Go की मूल बातें

Go Strings and the strings Package

Join, search, split, trim and slice text in Go.

  • शुरुआती
  • 2 मिनट में पढ़ें
  • 1 चलाने योग्य उदाहरण
आप क्या सीखेंगे
  • Building strings
  • Contains, Index, Split
  • Slicing a string
message[0:5]
H0
e1
l2
l3
o4
,5
The end index 5 is not included.

Read-only text with many helpers

A Go string is a read-only sequence of bytes, usually UTF-8 text. You cannot change a string in place, but you can build new ones, and the strings package has functions for most everyday jobs.

+= builds a new string. len counts bytes. Contains and Index search the text, Split cuts it at a separator and returns a slice, and message[0:5] takes the bytes from position 0 up to, but not including, 5.

मुख्य बातें

  1. A string cannot be changed in place.
  2. len counts bytes.
  3. message[0:5] takes bytes 0 to 4.

चलने योग्य Go उदाहरण

Go
कोड चलाएँ →
main.go
package main

import (
	"fmt"
	"strings"
)

func main() {
	message := "Hello, Go"
	message += "!"

	fmt.Println(message)
	fmt.Println(len(message), strings.ToUpper(message))
	fmt.Println(strings.Contains(message, "Go"), strings.Index(message, "Go"))
	fmt.Println(strings.Split("a,b,c", ","))
	fmt.Println(strings.Repeat("=", 5), strings.TrimSpace("  padded  "))
	fmt.Println(message[0:5], string(message[7]))
}
इस उदाहरण के लिए Run बटन बनाएँ कोड चलाने का परिणाम
Hello, Go!
10 HELLO, GO!
true 7
[a b c]
===== padded
Hello G

अभ्यास

उदाहरण को ऑनलाइन कंपाइलर में खोलें, कोई मान बदलें या एक पंक्ति जोड़ें, और चलाने से पहले आउटपुट का अनुमान लगाएँ। फिर कोड में जान-बूझकर गलती करें और कंपाइलर का संदेश पढ़ें।

यह पाठ पूरा कर लिया?

अपनी प्रगति ट्रैक करने के लिए इसे सीखा हुआ मार्क करें।

अगला: Reverse a String →

चैलेंज से अभ्यास करें

इस पाठ में सीखी बात से एक असली समस्या हल करें। हर चैलेंज ब्राउज़र में चलता है, तुरंत टेस्ट और AI रिव्यू के साथ।

सभी चैलेंज