In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use GO language basic data type string". In daily operation, I believe many people have doubts about how to use go language basic data type string. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt about "how to use GO language basic data type string". Next, please follow the editor to study!
String
Strings in the Go language appear as native data types. The internal implementation of strings in the Go language uses UTF-8 encoding. The value of the string is the content in double quotation marks ("), and non-ASCII characters can be added directly to the source code of GE language.
In GO, strings are wrapped in double quotation marks
In GO, single quotation marks wrap characters.
/ / string s: = "Hello China" / / A single letter, Chinese character, coincidence indicates a character C1: = 'h'c2: =' 1'c3: = 'medium' / / byte: 1 byte = 8Bit (8 binary bits) / / 1 character 'Avalanche 1 byte / / 1 utf8-encoded Chinese character' in'= generally occupies 3 bytes of string escape character
Escape character meaning
\ r carriage return (return to the beginning of the line)
\ nnewline character (skip directly to the same column position on the next line)
\ t Tab character
'single quotation marks
"double quotation marks
\ backslash
Common operations of strings
Method introduction
Len (str) to find the length
+ or fmt.Sprintf concatenation string
Strings.Split segmentation
Strings.contains determines whether to include or not
Strings.HasPrefix,strings.HasSuffix prefix / suffix judgment
Strings.Index (), the location of the strings.LastIndex () substring
Strings.Join (a [] string, sep string) join operation
Package mainimport ("fmt"strings") / / string func main () {/ /\ originally has a special meaning Telling the program\ is a simple\ path: = "\" E:\\ 36 Python full stack development materials\\ Administrator (8E5370323193)\\ preview (2)\ "path2: =" E:\\ 36 Python full stack development materials\\ Administrator (8E5370323193)\\ preview (2)'"fmt.Println (path) fmt.Println (path2) s: =" iTunm Ok "fmt.Println (s) / / Multi-line string S2: = 'the world is thin, human feelings and bad rain send dusk flowers and easy to fall' fmt.Println (S2) S3: = `E:\ 36 Python full stack development materials\ Administrator (8E5370323193)\ preview (2) `fmt.Println (S3) / / word String related operation fmt.Println (len (S3)) / / string concatenation name: = "ideal" world: = "ambitious" ss: = name + world fmt.Println (ss) ss1: = fmt.Sprintf ("% s% s" Name, world) / / fmt.Printf ("% s% s", name, world) fmt.Println (ss1) / / Segmentation ret: = strings.Split (S3, "\\") fmt.Println (ret) / / contains fmt.Println (strings.Contains (ss, "ideal")) / / prefix fmt.Println (strings.HasPrefix (ss) "ideal")) / / suffix fmt.Println (strings.HasSuffix (ss, "ideal") S4: = "abcded" fmt.Println (strings.Index (S4, "c")) fmt.Println (strings.LastIndex (S4, "c")) fmt.Println (strings.Index (S4, "d")) fmt.Println (strings.LastIndex (S4) "d")) / / stitching fmt.Println (strings.Join (ret, "+"))} byte and rune types
The characters are enclosed in single quotation marks (')
There are two types of characters in Go language:
The uint8 type, or byte type, represents a character of the ASCII code.
The rune type, which represents a UTF-8 character. The rune type is actually an int32
At the bottom of the string is a byte array that can be converted to and from the [] byte type. A string that cannot be modified is made up of byte bytes, so the length of the string is the length of byte bytes. The rune type is used to represent utf8 characters, and a rune character consists of one or more byte.
Modify string
To modify a string, you need to convert it to [] rune or [] byte, and then to string. Either way, the memory is reallocated and the byte array is copied.
Type conversion
The basic syntax for casting is as follows:
T (expression)
Where T represents the type to be converted. Expressions include variables, complex operators and function return values.
Package mainimport ("fmt") func main () {s: = "Hello China" / / len () is the number of byte bytes n: = len (s) fmt.Println (n) / / for I: = 0; I < len (s) ITunes + {/ / fmt.Println (s [I]) / / fmt.Printf ("% c\ n") S [I]) / /% c: character / /} / / string modification S2: = "white radish" / / [white radish] S3: = [] rune (S2) / / forcibly converts the string into a rune slice S3 [0] = 'red' / / single quotation marks represent the character fmt.Println ( String (S3)) / / forcibly convert rune slices to the string C1: = "red" c2: = 'red' / / rune (int32) fmt.Printf ("C1 int32% T c2rune% T\ n" C1, c2) / / c1:string c2:int32 c3: = "H" c4: = 'H'c5: = byte (' H') fmt.Printf ("c3fmt.Printf% T c4c3:string c4:int32 fmt.Printf% T\ n", c3, c4) / / c3:string c4:int32 fmt.Printf ("c4fmt.Printf% d\ n", c4) / / c4fmt.Printf 72 fmt.Printf ("c5fmt.Printf% T\ n") C5) / / c5:uint8 / / Type conversion N1: = 10 / / int var f float64 f = float64 (N1) fmt.Println (f) fmt.Printf ("% T\ n", f) / / float64} so far The study on "how to use strings of basic data types in GO language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.