fmt
10 methodsfmt 包实现了格式化 I/O,其函数类似于 C 的 printf 和 scanf。
strings.Contains(s, substr string) bool使用每个操作数的默认格式打印。当两个操作数都不是字符串时,会在操作数之间添加空格。
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | 要打印的操作数。 |
| substr | string | Substring to find. |
Returns
bool
Example
go
package main
import ("fmt"; "strings")
func main() {
fmt.Println(strings.Contains("hello", "ell")) // true
fmt.Println(strings.Contains("hello", "xyz")) // false
}strings.HasPrefix(s, prefix string) bool使用默认格式打印,操作数之间用空格分隔,末尾添加换行符。
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | 要打印的操作数。 |
| prefix | string | Prefix to test. |
Returns
bool
Example
go
strings.HasPrefix("hello.go", "hello") // true
strings.HasPrefix("hello.go", ".go") // falsestrings.HasSuffix(s, suffix string) bool根据格式说明符打印。
Parameters
| Name | Type | Description |
|---|---|---|
| format | string | 格式字符串(例如 %v、%d、%s)。 |
| suffix | string | 操作数。 |
Returns
bool
Example
go
strings.HasSuffix("hello.go", ".go") // true
strings.HasSuffix("hello.go", "hello") // falsestrings.Index(s, substr string) int根据格式说明符返回字符串(而非打印)。
Parameters
| Name | Type | Description |
|---|---|---|
| format | string | 格式字符串。 |
| substr | string | 操作数。 |
Returns
int
Example
go
strings.Index("hello", "ll") // 2
strings.Index("hello", "z") // -1strings.Split(s, sep string) []string根据格式说明符写入 w。
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | 写入目标。 |
| format | string | 格式字符串。 |
Returns
[]string
Example
go
strings.Split("a,b,c", ",") // ["a", "b", "c"]
strings.Split("hello", "") // ["h","e","l","l","o"]strings.Join(elems []string, sep string) string扫描从标准输入读取的文本,将连续的以空格分隔的值依次存入连续的参数中。
Parameters
| Name | Type | Description |
|---|---|---|
| format | []string | 格式字符串。 |
| sep | string | 用于接收值的指针。 |
Returns
string
Example
go
strings.Join([]string{"a", "b", "c"}, "-") // "a-b-c"
strings.Join([]string{"x"}, ",") // "x"strings.Replace(s, old, new string, n int) string返回一个根据格式说明符格式化的错误。使用 %w 进行包装。
Parameters
| Name | Type | Description |
|---|---|---|
| format | string | 格式字符串(使用 %w 包装错误)。 |
| old | string | 操作数。 |
| new | string | Replacement. |
| n | int | Max replacements (-1 for all). |
Returns
string
Example
go
strings.Replace("a-b-c", "-", "_", 1) // "a_b-c"
strings.Replace("a-b-c", "-", "_", -1) // "a_b_c"strings.ToUpper(s string) stringReturn s with all Unicode letters mapped to upper case.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | Source string. |
Returns
string
Example
go
strings.ToUpper("hello") // "HELLO"
strings.ToUpper("café") // "CAFÉ"strings.ToLower(s string) stringReturn s with all Unicode letters mapped to lower case.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | Source string. |
Returns
string
Example
go
strings.ToLower("HELLO") // "hello"
strings.ToLower("CAFÉ") // "café"strings.Trim(s, cutset string) stringReturn s with all leading and trailing characters in cutset removed.
Parameters
| Name | Type | Description |
|---|---|---|
| s | string | Source string. |
| cutset | string | Characters to trim. |
Returns
string
Example
go
strings.Trim(" hi ", " ") // "hi"
strings.Trim("xxhelloxx", "x") // "hello"