Merge commit '83e6afe6991b630b4ac0e48f5d6a06b658786796' into ja
This commit is contained in:
@@ -1,19 +1,20 @@
|
||||
# 7.6 Strings
|
||||
|
||||
Almost everything we see is represented by string, so it's a very important part of web development, including user inputs, database access; also we need to split, join and convert strings in many cases. In this section, we are going to introduce packages `strings` and `strconv` in Go standard library.
|
||||
On the web, almost everything we see (including user inputs, database access, etc.), is represented by strings. They are a very important part of web development. In many cases, we also need to split, join, convert and otherwise manipulate strings. In this section, we are going to introduce the `strings` and `strconv` packages from the Go standard library.
|
||||
|
||||
## strings
|
||||
|
||||
Following functions are from package `strings`, more details please see official documentation:
|
||||
The following functions are from the `strings` package. See the official documentation for more details:
|
||||
|
||||
- func Contains(s, substr string) bool
|
||||
|
||||
Check if string `s` contains string `substr`, returns boolean value.
|
||||
Check if string `s` contains string `substr`, returns a boolean value.
|
||||
|
||||
fmt.Println(strings.Contains("seafood", "foo"))
|
||||
fmt.Println(strings.Contains("seafood", "bar"))
|
||||
fmt.Println(strings.Contains("seafood", ""))
|
||||
fmt.Println(strings.Contains("", ""))
|
||||
|
||||
//Output:
|
||||
//true
|
||||
//false
|
||||
@@ -39,14 +40,14 @@ Following functions are from package `strings`, more details please see official
|
||||
|
||||
- func Repeat(s string, count int) string
|
||||
|
||||
Repeat string `s` with `count` times.
|
||||
Repeat string `s` `count` times.
|
||||
|
||||
fmt.Println("ba" + strings.Repeat("na", 2))
|
||||
//Output:banana
|
||||
|
||||
- func Replace(s, old, new string, n int) string
|
||||
|
||||
Replace string `old` with string `new` in string `s`, `n` means replication times, if n less than 0 means replace all.
|
||||
Replace string `old` with string `new` in string `s`. `n` is the number of replacements. If n is less than 0, replace all instances.
|
||||
|
||||
fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
|
||||
fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
|
||||
@@ -75,7 +76,7 @@ Following functions are from package `strings`, more details please see official
|
||||
|
||||
- func Fields(s string) []string
|
||||
|
||||
Remove space items and split string with space in to a slice.
|
||||
Remove space items and split string with space into a slice.
|
||||
|
||||
fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
|
||||
//Output:Fields are: ["foo" "bar" "baz"]
|
||||
@@ -83,9 +84,9 @@ Following functions are from package `strings`, more details please see official
|
||||
|
||||
## strconv
|
||||
|
||||
Following functions are from package `strconv`, more details please see official documentation:
|
||||
The following functions are from the `strconv` package. As usual, please see official documentation for more details:
|
||||
|
||||
- Append series convert data to string and append to current byte slice.
|
||||
- Append series, convert data to string, and append to current byte slice.
|
||||
|
||||
package main
|
||||
|
||||
@@ -103,7 +104,7 @@ Following functions are from package `strconv`, more details please see official
|
||||
fmt.Println(string(str))
|
||||
}
|
||||
|
||||
- Format series convert other type data to string.
|
||||
- Format series, convert other data types into string.
|
||||
|
||||
package main
|
||||
|
||||
@@ -121,7 +122,7 @@ Following functions are from package `strconv`, more details please see official
|
||||
fmt.Println(a, b, c, d, e)
|
||||
}
|
||||
|
||||
- Parse series convert string to other types.
|
||||
- Parse series, convert strings to other types.
|
||||
|
||||
package main
|
||||
|
||||
@@ -158,4 +159,4 @@ Following functions are from package `strconv`, more details please see official
|
||||
|
||||
- [Directory](preface.md)
|
||||
- Previous section: [Files](07.5.md)
|
||||
- Next section: [Summary](07.7.md)
|
||||
- Next section: [Summary](07.7.md)
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
# 7.7 Summary
|
||||
|
||||
In this chapter, we introduced some text process tools like XML, JSON, Regexp and template. XML and JSON are data exchange tools, if you can represent almost all kinds of information though these two formats. Regexp is a powerful tool for searching, replacing, cutting text content. With template, you can easily combine dynamic data with static files. These tools are all useful when you develop web application, I hope you understand more about processing and showing content.
|
||||
In this chapter, we introduced some text processing tools like XML, JSON, Regexp and we also talked about templates. XML and JSON are data exchange tools. You can represent almost any kind of information using these two formats. Regexp is a powerful tool for searching, replacing and cutting text content. With templates, you can easily combine dynamic data with static files. These tools are all useful when developping web applications. I hope that you now have a better understanding of processing and showing content using Go.
|
||||
|
||||
## Links
|
||||
|
||||
- [Directory](preface.md)
|
||||
- Previous section: [Strings](07.6.md)
|
||||
- Next chapter: [Web services](08.0.md)
|
||||
- Next chapter: [Web services](08.0.md)
|
||||
|
||||
Reference in New Issue
Block a user