खोज…


परिचय

एक स्ट्रिंग बाइट्स के केवल-पढ़ने वाले स्लाइस के प्रभाव में है। गो में एक स्ट्रिंग शाब्दिक में हमेशा इसकी सामग्री का एक वैध UTF-8 प्रतिनिधित्व होगा।

वाक्य - विन्यास

  • चर नाम: = "हैलो वर्ल्ड" // एक स्ट्रिंग घोषित करें
  • चर नाम: = `हैलो वर्ल्ड` // एक कच्चे शाब्दिक स्ट्रिंग की घोषणा करें
  • चर नाम: = "नमस्कार" + "दुनिया" // तार को समेटना
  • प्रतिस्थापन: = "हैलो वर्ल्ड" [0: 4] // स्ट्रिंग का एक हिस्सा मिलता है
  • पत्र: = "हैलो वर्ल्ड" [6] // स्ट्रिंग का एक चरित्र मिलता है
  • fmt.Sprintf ("% s", "हैलो वर्ल्ड") // एक स्ट्रिंग प्रारूपित करता है

स्ट्रिंग प्रकार

string प्रकार आपको पाठ को संग्रहीत करने की अनुमति देता है, जो वर्णों की एक श्रृंखला है। तार बनाने के कई तरीके हैं। डबल कोट्स के बीच टेक्स्ट लिखकर एक शाब्दिक स्ट्रिंग बनाई जाती है।

text := "Hello World"

क्योंकि गो स्ट्रिंग्स UTF-8 का समर्थन करते हैं, पिछला उदाहरण पूरी तरह से मान्य है। स्ट्रिंग्स मनमाने ढंग से बाइट्स रखती हैं, जिसका मतलब यह नहीं है कि हर स्ट्रिंग में वैध UTF-8 होगा, लेकिन स्ट्रिंग शाब्दिक हमेशा मान्य UTF-8 क्रम धारण करेगा।

तार का शून्य मान एक रिक्त स्ट्रिंग है ""

+ ऑपरेटर का उपयोग करके स्ट्रिंग्स को समतल किया जा सकता है।

text := "Hello " + "World"

स्ट्रिंग को `` का उपयोग करके भी परिभाषित किया जा सकता है। यह एक कच्चा स्ट्रिंग शाब्दिक बनाता है जिसका अर्थ है कि पात्र बच नहीं पाएंगे।

text1 := "Hello\nWorld"
text2 := `Hello
World`

पिछले उदाहरण में, text1 \n वर्ण से बच जाता है जो एक नई रेखा का प्रतिनिधित्व करता है जबकि text2 में नई पंक्ति वर्ण सीधे होता है। यदि आप text1 == text2 की तुलना करते हैं तो परिणाम true होगा।

हालाँकि, text2 := `Hello\nWorld` \n वर्ण से नहीं बच पाएगा, जिसका अर्थ है कि स्ट्रिंग में नई लाइन के बिना पाठ Hello\nWorld text2 := `Hello\nWorld` शामिल है। यह text1 := "Hello\\nWorld" टाइप करने के बराबर होगा text1 := "Hello\\nWorld"

पाठ स्वरूपण

पैकेज fmt लागू करने के लिए प्रारूप क्रिया का उपयोग करके पाठ को प्रिंट और प्रारूपित करता है । क्रियाओं को एक प्रतिशत चिह्न के साथ दर्शाया जाता है।

सामान्य क्रियाएं:

%v    // the value in a default format
      // when printing structs, the plus flag (%+v) adds field names
%#v   // a Go-syntax representation of the value
%T    // a Go-syntax representation of the type of the value
%%    // a literal percent sign; consumes no value

बूलियन:

%t    // the word true or false

पूर्णांक:

%b    // base 2
%c    // the character represented by the corresponding Unicode code point
%d    // base 10
%o    // base 8
%q    // a single-quoted character literal safely escaped with Go syntax.
%x    // base 16, with lower-case letters for a-f
%X    // base 16, with upper-case letters for A-F
%U    // Unicode format: U+1234; same as "U+%04X"

फ़्लोटिंग-पॉइंट और जटिल घटक:

%b    // decimalless scientific notation with exponent a power of two,
      // in the manner of strconv.FormatFloat with the 'b' format,
      // e.g. -123456p-78
%e    // scientific notation, e.g. -1.234456e+78
%E    // scientific notation, e.g. -1.234456E+78
%f    // decimal point but no exponent, e.g. 123.456
%F    // synonym for %f
%g    // %e for large exponents, %f otherwise
%G    // %E for large exponents, %F otherwise

स्ट्रिंग और बाइट्स का टुकड़ा (इन क्रियाओं के साथ समान रूप से व्यवहार किया जाता है):

%s    // the uninterpreted bytes of the string or slice
%q    // a double-quoted string safely escaped with Go syntax
%x    // base 16, lower-case, two characters per byte
%X    // base 16, upper-case, two characters per byte

सूचक:

%p    // base 16 notation, with leading 0x

क्रियाओं का उपयोग करते हुए, आप कई प्रकारों को बनाते हुए तार बना सकते हैं:

text1 := fmt.Sprintf("Hello %s", "World")
text2 := fmt.Sprintf("%d + %d = %d", 2, 3, 5)
text3 := fmt.Sprintf("%s, %s (Age: %d)", "Obama", "Barack", 55)

फ़ंक्शन Sprintf पहले पैरामीटर में स्ट्रिंग को स्वरूपित करता है जो क्रियाओं को अगले मापदंडों में मानों के साथ प्रतिस्थापित करता है और परिणाम देता है। जैसा Sprintf , समारोह Printf भी प्रारूपों लेकिन इसके बजाय परिणाम यह स्ट्रिंग प्रिंट लौटने का।

तार पैकेज

  • strings.Contains

    fmt.Println(strings.Contains("foobar", "foo")) // true
    fmt.Println(strings.Contains("foobar", "baz")) // false
    
  • strings.HasPrefix

    fmt.Println(strings.HasPrefix("foobar", "foo")) // true
    fmt.Println(strings.HasPrefix("foobar", "baz")) // false
    
  • strings.HasSuffix

    fmt.Println(strings.HasSuffix("foobar", "bar")) // true
    fmt.Println(strings.HasSuffix("foobar", "baz")) // false
    
  • strings.Join

    ss := []string{"foo", "bar", "bar"}
    fmt.Println(strings.Join(ss, ", ")) // foo, bar, baz
    
  • strings.Replace

    fmt.Println(strings.Replace("foobar", "bar", "baz", 1)) // foobaz
    
  • strings.Split

    s := "foo, bar, bar"
    fmt.Println(strings.Split(s, ", ")) // [foo bar baz]
    
  • strings.ToLower

    fmt.Println(strings.ToLower("FOOBAR")) // foobar
    
  • strings.ToUpper

    fmt.Println(strings.ToUpper("foobar")) // FOOBAR
    
  • strings.TrimSpace

    fmt.Println(strings.TrimSpace("  foobar  ")) // foobar
    

अधिक: https://golang.org/pkg/strings/



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow