In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use Go language strings", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use Go language strings" this article.
Contains Contains (sQuery substr string) bool
Determine whether s contains substr true: contains false: does not contain
Example:
Str: = "hello word" fmt.Println (strings.Contains (str, "hello")) / / true fmt.Println (strings.Contains (str, "I")) / / false fmt.Println (strings.Contains (",")) / / true pay special attention to ContainsAny (s, chars string) bool here
Determine whether the word s contains any of the characters in chars. True: contains false: does not contain
Example:
Str: = "hello word" fmt.Println (strings.ContainsAny (str, "w")) / / true fmt.Println (strings.ContainsAny (str, ")) / / false fmt.Println (strings.ContainsAny (str," def ")) / / true fmt.Println (strings.ContainsAny (", ")) / / false Statistics Count (s, substr string) int
Count the total number of occurrences of the character substr in the string s
Example:
Str: = "hello word" fmt.Println (strings.Count (str, "l")) / / 2 fmt.Println (strings.Count (str, ")) / / 1 fmt.Println (strings.Count (str,")) / / 11 each string is followed by an EOF Terminator comparing EqualFold (s, t string) bool
Determine whether the string s is all equal to the string t and is not case-sensitive
Example:
Str: = "hello word" fmt.Println (strings.EqualFold (str, "hello word")) / / true fmt.Println (strings.EqualFold (str, "hello")) / / false fmt.Println (strings.EqualFold (str, "HELLO WORD")) / / trueHasPrefix (s, prefix string) bool
Determine whether the string s begins with the character prefix. If it returns true, it returns false.
Example:
Str: = "hello word" fmt.Println (strings.HasPrefix (str, "hello")) / / true fmt.Println (strings.HasPrefix (str, "hellow")) / / falseHasSuffix (s, suffix string) bool
Determine whether the string s ends with the character suffix, and return false if it returns true
Example:
Str: = "hello word" fmt.Println (strings.HasSuffix (str, "word")) / / true fmt.Println (strings.HasSuffix (str, "word")) / / true fmt.Println (strings.HasSuffix (str, "word")) / / false split Fields (s string) [] string
Split the string s into several strings with a space as a separator, and return the sliced string if successful, otherwise return the original string
Example:
Str: = "helloword" fmt.Println (strings.Fields (str)) fmt.Println (strings.Fields ("helloword")) subscript Index (s, sep string) int
Determines where the character sep first appears in the string s, returns the index of the sep position if successful, and returns-1 if the character sep is not in the string s
Example:
Str: = "hello word" fmt.Println (strings.Index (str, "hello")) / / 0 fmt.Println (strings.Index (str, "word")) / / 6 fmt.Println (strings.Index (str, "word")) / /-1 fmt.Println (strings.Index (str, ")) / / 0 fmt.Println (strings.Index (str,")) / / 5func LastIndex (s, sep string) int
Returns the position of the last occurrence of the substring sep in the string s, or-1 if it cannot be found. If sep is empty, the length of the string is returned.
Example:
Str: = "hello word" fmt.Println (strings.LastIndex (str, "d")) / / 9 fmt.Println (strings.LastIndex (str, "l")) / / 3 fmt.Println (strings.LastIndex (str, "word")) / /-1func IndexAny (s, chars string) int
Returns the position where any character in the string chars appears for the first time in the string s, or-1 if it cannot be found, or-1 if chars is empty
Example:
Str: = "hello word" fmt.Println (strings.IndexAny (str, "helo")) / / 0 fmt.Println (strings.IndexAny (str, "ws")) / / 6 splicing func Join (a [] string, sep string) string
Join concatenates the substrings in an into a separate string, separated by sep
Example:
Fmt.Println (strings.Join ([] string {"a", "b", "c"}, "-")) / a-b-c fmt.Println (strings.Join ([] string {"a", "b", "c"}, "/")) / / a/b/cfunc Repeat (s string, count int) string
Concatenate count string s into a new string
Example:
Str: = "hello word" fmt.Println (strings.Repeat (str, 2)) case func ToUpper (s string) string
Modify all characters in s to their uppercase format. For non-ASCII characters, their uppercase format needs to be looked up and converted.
Example:
Str: = "hello word" fmt.Println (strings.ToUpper (str)) func ToLower (s string) string
Modify all characters in s to their lowercase format. For non-ASCII characters, their lowercase format needs to be looked up and converted.
Example:
Str: = "hello word" fmt.Println (strings.ToLower (str)) unblanked func Trim (s string, cutset string) string
The consecutive s characters contained in the cutset will be deleted.
Example:
Fmt.Println (strings.Trim (str, "hello")) / / word str = "hello word hello" fmt.Println (strings.Trim (str, "hello")) / / wordfunc TrimLeft (s string, cutset string) string
TrimLeft will delete the consecutive characters contained in the cutset with the s header
Example:
Str = "hello word" fmt.Println (strings.TrimLeft (str, "hello")) / / word str = "hello word hello" fmt.Println (strings.TrimLeft (str, "hello")) / / word hellofunc TrimRight (s string, cutset string) string
TrimRight will delete the consecutive characters contained in the cutset at the end of s.
Example:
Str = "hello word hello123" fmt.Println (strings.TrimRight (str, "hello123")) / / hello word fmt.Println (strings.TrimRight (str, "123")) / / hello word hello is all the content of this article "how to use Go language strings". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.