Introduction to golang string operation instances

Source: Internet
Author: User
Tags gopher

This article will introduce you to the golang string operation examples. I hope this method will be helpful to you.


Example 1

The Code is as follows: Copy code

Package main

Import s "strings"

// Alias
Import (
"Fmt"
)

Var p = fmt. Println

Func main (){
P ("Contains:", s. Contains ("test", "es") // whether to include true
P ("Count:", s. Count ("test", "t") // number of times a string contains 2 Characters
P ("HasPrefix:", s. HasPrefix ("test", "te") // judge whether the string header is true.
P ("HasSuffix:", s. HasSuffix ("test", "st") // judge whether the string ends with true
P ("Index:", s. Index ("test", "e") // query string location 1
P ("Join:", s. Join ([] string {"a", "B"}, "-") // concatenate a-B from a string array
P ("Repeat:", s. Repeat ("a", 5) // Repeat a string aaaaa
P ("Replace:", s. replace ("foo", "o", "0",-1) // Replace string with the f00 if the starting position is smaller than 0.
P ("Replace:", s. Replace ("foo", "o", "0", 1) // string replacement specifies the starting position of 1 f0o
P ("Split:", s. Split ("a-B-c-d-e", "-") // string cutting [a B c d e]
P ("ToLower:", s. ToLower ("TEST") // converts the string to test in lowercase.
P ("ToUpper:", s. ToUpper ("test") // converts string uppercase to TEST
P ()
P ("Len:", len ("hello") // String Length
P ("Char:", "hello" [1]) // mark the character in the string, type: byte
}

Example

The Code is as follows: Copy code

2 package main

Import (
"Fmt"
"Strings"
// "Unicode/utf8"
)

Func main (){
Fmt. Println ("checking whether the substring is in the specified string ")
Fmt. Println ("Contains function usage ")
Fmt. Println (strings. Contains ("seafood", "foo") // true
Fmt. Println (strings. Contains ("seafood", "bar") // false
Fmt. Println (strings. Contains ("seafood", "") // true
Fmt. Println (strings. Contains ("", "") // true special attention here
Fmt. Println (strings. Contains ("I am Chinese", "I") // true

Fmt. Println ("")
Fmt. Println ("ContainsAny function usage ")
Fmt. Println (strings. ContainsAny ("team", "I") // false
Fmt. Println (strings. ContainsAny ("failure", "u & I") // true
Fmt. Println (strings. ContainsAny ("foo", "") // false
Fmt. Println (strings. ContainsAny ("", "") // false

Fmt. Println ("")
Fmt. Println ("ContainsRune function usage ")
Fmt. Println (strings. ContainsRune ("I am China", 'I') // true pay attention to the second parameter, which is a character

Fmt. Println ("")
Fmt. Println ("Count function usage ")
Fmt. Println (strings. Count ("cheese", "e") // 3
Fmt. Println (strings. Count ("five", "") // before & after each rune result: 5, implemented in source code

Fmt. Println ("")
Fmt. Println ("usage of implicit fold function ")
Fmt. Println (strings. Character fold ("Go", "go") // case insensitive

Fmt. Println ("")
Fmt. Println ("Fields function usage ")
Fmt. Println ("Fields are: % q", strings. Fields ("foo bar baz") // ["foo" "bar" "baz"] returns a list

// Equivalent to using a function as a parameter and supporting anonymous Functions
For _, record: = range [] string {"aaa * 1892*122", "aaataat", "124 | 939 | 22 "}{
Fmt. Println (strings. FieldsFunc (record, func (ch rune) bool {
Switch {
Case ch> '5 ':
Return true
}
Return false
}))
}

Fmt. Println ("")
Fmt. Println ("HasPrefix function usage ")
Fmt. Println (strings. HasPrefix ("NLT_abc", "NLT") // The prefix starts with NLT

Fmt. Println ("")
Fmt. Println ("HasSuffix function usage ")
Fmt. Println (strings. HasSuffix ("NLT_abc", "abc") // The suffix starts with NLT.

Fmt. Println ("")
Fmt. Println ("Index function usage ")
Fmt. Println (strings. Index ("NLT_abc", "abc") // returns the position of the first matched character. Here is 4
Fmt. Println (strings. Index ("NLT_abc", "aaa") // If yes-1 is returned
Fmt. Println (strings. Index ("I am a Chinese", "medium") // if there is an error, 6 is returned.

Fmt. Println ("")
Fmt. Println ("IndexAny function usage ")
Fmt. Println (strings. IndexAny ("I am a Chinese", "medium") // if there is a returned 6
Fmt. Println (strings. IndexAny ("I am a Chinese", "and") // returns-1 in the Presence

Fmt. Println ("")
Fmt. Println ("Index function usage ")
Fmt. Println (strings. IndexRune ("NLT_abc", 'B') // returns the position of the first matched character. Here is 4
Fmt. Println (strings. IndexRune ("NLT_abc", 's') // if an existing one returns-1
Fmt. Println (strings. IndexRune ("I am a Chinese", 'zhong') // if the object exists, 6 is returned.

Fmt. Println ("")
Fmt. Println ("Join function usage ")
S: = [] string {"foo", "bar", "baz "}
Fmt. Println (strings. Join (s, ",") // return string: foo, bar, baz

Fmt. Println ("")
Fmt. Println ("LastIndex function usage ")
Fmt. Println (strings. LastIndex ("go gopher", "go") // 3

Fmt. Println ("")
Fmt. Println ("LastIndexAny function usage ")
Fmt. Println (strings. LastIndexAny ("go gopher", "go") // 4
Fmt. Println (strings. LastIndexAny ("I am a Chinese", "medium") // 6

Fmt. Println ("")
Fmt. Println ("Map function usage ")
Rot13: = func (r rune) rune {
Switch {
Case r> = 'A' & r <= 'Z ':
Return 'A' + (r-'A' + 13) % 26
Case r> = 'A' & r <= 'Z ':
Return 'A' + (r-'A' + 13) % 26
}
Return r
}
Fmt. Println (strings. Map (rot13, "'Twas brillig and the slithy gopher ..."))

Fmt. Println ("")
Fmt. Println ("Repeat function usage ")
Fmt. Println ("ba" + strings. Repeat ("na", 2) // banana

Fmt. Println ("")
Fmt. Println ("Replace function usage ")
Fmt. Println (strings. Replace ("oink", "k", "ky", 2 ))
Fmt. Println (strings. Replace ("oink", "oink", "moo",-1 ))

Fmt. Println ("")
Fmt. Println ("usage of the Split function ")
Fmt. Printf ("% qn", strings. Split ("a, B, c ",","))
Fmt. Printf ("% qn", strings. Split ("a man a plan a canal panama", ""))
Fmt. Printf ("% qn", strings. Split ("xyz ",""))
Fmt. Printf ("% qn", strings. Split ("", "Bernardo O 'Higgins "))

Fmt. Println ("")
Fmt. Println ("SplitAfter function usage ")
Fmt. printf ("% qn", strings. splitAfter ("/home/m_ta/src", "/") // ["/" home/"m_ta/" "src"]

Fmt. Println ("")
Fmt. Println ("SplitAfterN function usage ")
Fmt. printf ("% qn", strings. splitAfterN ("/home/m_ta/src", "/", 2) // ["/" home/m_ta/src "]
Fmt. printf ("% qn", strings. splitAfterN ("# home # m_ta # src", "#",-1) // ["/" home/"" m_ta/"src"]

Fmt. Println ("")
Fmt. Println ("SplitN function usage ")
Fmt. Printf ("% qn", strings. SplitN ("/home/m_ta/src", "/", 1 ))

Fmt. printf ("% qn", strings. splitN ("/home/m_ta/src", "/", 2) // ["/" home/"" m_ta/"" src "]
Fmt. printf ("% qn", strings. splitN ("/home/m_ta/src", "/",-1) // ["" home "" m_ta "src"]
Fmt. printf ("% qn", strings. splitN ("home, m_ta, src", 2) // ["/" "home/" "m_ta/" "src"]

Fmt. printf ("% qn", strings. splitN ("# home # m_ta # src", "#",-1) // ["/" home/"" m_ta/"" src "]

Fmt. Println ("")
Fmt. Println ("Title function usage") // This function does not know how to use it.
Fmt. Println (strings. Title ("her royal highness "))

Fmt. Println ("")
Fmt. Println ("ToLower function usage ")
Fmt. Println (strings. ToLower ("Gopher") // gopher

Fmt. Println ("")
Fmt. Println ("ToLowerSpecial function usage ")

Fmt. Println ("")
Fmt. Println ("ToTitle function usage ")
Fmt. Println (strings. ToTitle ("your noises "))
Fmt. Println (strings. ToTitle ("Hangzhou China "))

Fmt. Println ("")
Fmt. Println ("Replace function usage ")
Fmt. Println (strings. Replace ("ABAACEDF", "A", "a", 2) // aBaACEDF
// If the fourth parameter is smaller than 0, all parameters are replaced. You can refer to golang's document.
Fmt. Println (strings. Replace ("ABAACEDF", "A", "a",-1) // aBaaCEDF

Fmt. Println ("")
Fmt. Println ("ToUpper function usage ")
Fmt. Println (strings. ToUpper ("Gopher") // GOPHER

Fmt. Println ("")
Fmt. Println ("Trim function usage ")
Fmt. Printf ("[% q]", strings. Trim ("!!! Achtung !!! ","! ") // [" Achtung "]

Fmt. Println ("")
Fmt. Println ("TrimLeft function usage ")
Fmt. Printf ("[% q]", strings. TrimLeft ("!!! Achtung !!! ","! ") // [" Achtung !!! "]

Fmt. Println ("")
Fmt. Println ("TrimSpace function usage ")
Fmt. Println (strings. TrimSpace ("tn a lone gopher ntrn") // a lone gopher

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.