Documentation
¶
Overview ¶
Package strings implements simple functions to manipulate UTF-8 encoded strings.
For information about UTF-8 strings in Go, see https://blog.golang.org/strings.
Index ¶
- func Clone(s string) string
- func Compare(a, b string) int
- func Contains(s, substr string) bool
- func ContainsAny(s, chars string) bool
- func ContainsFunc(s string, f func(rune) bool) bool
- func ContainsRune(s string, r rune) bool
- func Count(s, substr string) int
- func Cut(s, sep string) (before, after string, found bool)
- func CutPrefix(s, prefix string) (after string, found bool)
- func CutSuffix(s, suffix string) (before string, found bool)
- func EqualFold(s, t string) bool
- func Fields(s string) []string
- func FieldsFunc(s string, f func(rune) bool) []string
- func HasPrefix(s, prefix string) bool
- func HasSuffix(s, suffix string) bool
- func Index(s, substr string) int
- func IndexAny(s, chars string) int
- func IndexByte(s string, c byte) int
- func IndexFunc(s string, f func(rune) bool) int
- func IndexRune(s string, r rune) int
- func Join(elems []string, sep string) string
- func LastIndex(s, substr string) int
- func LastIndexAny(s, chars string) int
- func LastIndexByte(s string, c byte) int
- func LastIndexFunc(s string, f func(rune) bool) int
- func Map(mapping func(rune) rune, s string) string
- func Repeat(s string, count int) string
- func Replace(s, old, new string, n int) string
- func ReplaceAll(s, old, new string) string
- func Split(s, sep string) []string
- func SplitAfter(s, sep string) []string
- func SplitAfterN(s, sep string, n int) []string
- func SplitN(s, sep string, n int) []string
- func Title(s string) stringdeprecated
- func ToLower(s string) string
- func ToLowerSpecial(c unicode.SpecialCase, s string) string
- func ToTitle(s string) string
- func ToTitleSpecial(c unicode.SpecialCase, s string) string
- func ToUpper(s string) string
- func ToUpperSpecial(c unicode.SpecialCase, s string) string
- func ToValidUTF8(s, replacement string) string
- func Trim(s, cutset string) string
- func TrimFunc(s string, f func(rune) bool) string
- func TrimLeft(s, cutset string) string
- func TrimLeftFunc(s string, f func(rune) bool) string
- func TrimPrefix(s, prefix string) string
- func TrimRight(s, cutset string) string
- func TrimRightFunc(s string, f func(rune) bool) string
- func TrimSpace(s string) string
- func TrimSuffix(s, suffix string) string
- type Builder
- func (b *Builder) Cap() int
- func (b *Builder) Grow(n int)
- func (b *Builder) Len() int
- func (b *Builder) Reset()
- func (b *Builder) String() string
- func (b *Builder) Write(p []byte) (int, error)
- func (b *Builder) WriteByte(c byte) error
- func (b *Builder) WriteRune(r rune) (int, error)
- func (b *Builder) WriteString(s string) (int, error)
- type Reader
- func (r *Reader) Len() int
- func (r *Reader) Read(b []byte) (n int, err error)
- func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)
- func (r *Reader) ReadByte() (byte, error)
- func (r *Reader) ReadRune() (ch rune, size int, err error)
- func (r *Reader) Reset(s string)
- func (r *Reader) Seek(offset int64, whence int) (int64, error)
- func (r *Reader) Size() int64
- func (r *Reader) UnreadByte() error
- func (r *Reader) UnreadRune() error
- func (r *Reader) WriteTo(w io.Writer) (n int64, err error)
- type Replacer
Examples ¶
- Builder
- Clone
- Compare
- Contains
- ContainsAny
- ContainsRune
- Count
- Cut
- CutPrefix
- CutSuffix
- EqualFold
- Fields
- FieldsFunc
- HasPrefix
- HasSuffix
- Index
- IndexAny
- IndexByte
- IndexFunc
- IndexRune
- Join
- LastIndex
- LastIndexAny
- LastIndexByte
- LastIndexFunc
- Map
- NewReplacer
- Repeat
- Replace
- ReplaceAll
- Split
- SplitAfter
- SplitAfterN
- SplitN
- Title
- ToLower
- ToLowerSpecial
- ToTitle
- ToTitleSpecial
- ToUpper
- ToUpperSpecial
- ToValidUTF8
- Trim
- TrimFunc
- TrimLeft
- TrimLeftFunc
- TrimPrefix
- TrimRight
- TrimRightFunc
- TrimSpace
- TrimSuffix
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶ added in v1.18.0
Cloneは、sの新しいコピーを返します。 sを新しい割り当てにコピーすることを保証します。 これは、大きな文字列の小さなサブストリングのみを保持する場合に重要な場合があります。 Cloneを使用することで、このようなプログラムがより少ないメモリを使用できるようになります。 もちろん、Cloneを使用するとコピーが作成されるため、Cloneの過剰使用はプログラムのメモリ使用量を増やす可能性があります。 Cloneは通常、プロファイリングによって必要であることが示された場合にのみ使用する必要があります。 長さがゼロの文字列の場合、文字列 "" が返され、割り当ては行われません。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" "github.com/shogo82148/std/unsafe" ) func main() { s := "abc" clone := strings.Clone(s) fmt.Println(s == clone) fmt.Println(unsafe.StringData(s) == unsafe.StringData(clone)) }
Output: true false
func Compare ¶ added in v1.5.0
Compareは、2つの文字列を辞書順で比較して整数を返します。 a == bの場合は0、a < bの場合は-1、a > bの場合は+1になります。
Compareは、パッケージbytesとの対称性のために含まれています。 通常、組み込みの文字列比較演算子==、<、>などを使用する方が明確で、常に高速です。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.Compare("a", "b")) fmt.Println(strings.Compare("a", "a")) fmt.Println(strings.Compare("b", "a")) }
Output: -1 0 1
func Contains ¶
Containsは、substrがs内に含まれているかどうかを報告します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.Contains("seafood", "foo")) fmt.Println(strings.Contains("seafood", "bar")) fmt.Println(strings.Contains("seafood", "")) fmt.Println(strings.Contains("", "")) }
Output: true false true true
func ContainsAny ¶
ContainsAnyは、chars内の任意のUnicodeコードポイントがs内に含まれているかどうかを報告します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.ContainsAny("team", "i")) fmt.Println(strings.ContainsAny("fail", "ui")) fmt.Println(strings.ContainsAny("ure", "ui")) fmt.Println(strings.ContainsAny("failure", "ui")) fmt.Println(strings.ContainsAny("foo", "")) fmt.Println(strings.ContainsAny("", "")) }
Output: false true true true false false
func ContainsFunc ¶ added in v1.21.0
ContainsFuncは、s内の任意のUnicodeコードポイントrがf(r)を満たすかどうかを報告します。
func ContainsRune ¶
ContainsRuneは、Unicodeコードポイントrがs内に含まれているかどうかを報告します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { // 文字列が特定のUnicodeコードポイントを含むかどうかを検索します。 // たとえば、小文字の"a"のコードポイントは97です。 fmt.Println(strings.ContainsRune("aardvark", 97)) fmt.Println(strings.ContainsRune("timeout", 97)) }
Output: true false
func Count ¶
Countは、s内の重複しないsubstrのインスタンス数を数えます。 substrが空の文字列の場合、Countはs内のUnicodeコードポイントの数に1を加えたものを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.Count("cheese", "e")) fmt.Println(strings.Count("five", "")) // before & after each rune }
Output: 3 5
func Cut ¶ added in v1.18.0
最初の sep のインスタンスを中心に s をスライスし、 sep の前と後のテキストを返します。 found は、sep が s に現れるかどうかを報告します。 sep が s に現れない場合、cut は s、""、false を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { show := func(s, sep string) { before, after, found := strings.Cut(s, sep) fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found) } show("Gopher", "Go") show("Gopher", "ph") show("Gopher", "er") show("Gopher", "Badger") }
Output: Cut("Gopher", "Go") = "", "pher", true Cut("Gopher", "ph") = "Go", "er", true Cut("Gopher", "er") = "Goph", "", true Cut("Gopher", "Badger") = "Gopher", "", false
func CutPrefix ¶ added in v1.20.0
CutPrefix は、指定された先頭接頭辞文字列を除いた s を返し、接頭辞が見つかったかどうかを報告します。 s が prefix で始まらない場合、CutPrefix は s、false を返します。 prefix が空の文字列の場合、CutPrefix は s、true を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { show := func(s, sep string) { after, found := strings.CutPrefix(s, sep) fmt.Printf("CutPrefix(%q, %q) = %q, %v\n", s, sep, after, found) } show("Gopher", "Go") show("Gopher", "ph") }
Output: CutPrefix("Gopher", "Go") = "pher", true CutPrefix("Gopher", "ph") = "Gopher", false
func CutSuffix ¶ added in v1.20.0
CutSuffix は、指定された末尾接尾辞文字列を除いた s を返し、接尾辞が見つかったかどうかを報告します。 s が suffix で終わらない場合、CutSuffix は s、false を返します。 suffix が空の文字列の場合、CutSuffix は s、true を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { show := func(s, sep string) { before, found := strings.CutSuffix(s, sep) fmt.Printf("CutSuffix(%q, %q) = %q, %v\n", s, sep, before, found) } show("Gopher", "Go") show("Gopher", "er") }
Output: CutSuffix("Gopher", "Go") = "Gopher", false CutSuffix("Gopher", "er") = "Goph", true
func EqualFold ¶
EqualFoldは、UTF-8文字列として解釈されたsとtが、単純なUnicodeの大文字小文字を区別しない比較において等しいかどうかを報告します。 これは、大文字小文字を区別しない形式の大文字小文字を区別しない性質です。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.EqualFold("Go", "go")) fmt.Println(strings.EqualFold("AB", "ab")) // 単純なケースフォールディングを使用した比較のためtrue fmt.Println(strings.EqualFold("ß", "ss")) // 完全なケースフォールディングを使用しないためfalse }
Output: true true false
func Fields ¶
Fieldsは、sをUnicode.IsSpaceによって定義される1つ以上の連続する空白文字の各インスタンスで分割し、sの部分文字列のスライスまたは空のスライスを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz ")) }
Output: Fields are: ["foo" "bar" "baz"]
func FieldsFunc ¶
FieldsFuncは、Unicodeコードポイントcがf(c)を満たす連続するランで文字列sを分割し、sのスライスの配列を返します。 sのすべてのコードポイントがf(c)を満たすか、文字列が空の場合、空のスライスが返されます。
FieldsFuncは、f(c)を呼び出す順序について保証せず、fが常に同じ値を返すことを前提としています。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" "github.com/shogo82148/std/unicode" ) func main() { f := func(c rune) bool { return !unicode.IsLetter(c) && !unicode.IsNumber(c) } fmt.Printf("Fields are: %q", strings.FieldsFunc(" foo1;bar2,baz3...", f)) }
Output: Fields are: ["foo1" "bar2" "baz3"]
func HasPrefix ¶
HasPrefixは、文字列sがprefixで始まるかどうかをテストします。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.HasPrefix("Gopher", "Go")) fmt.Println(strings.HasPrefix("Gopher", "C")) fmt.Println(strings.HasPrefix("Gopher", "")) }
Output: true false true
func HasSuffix ¶
HasSuffixは、文字列sがsuffixで終わるかどうかをテストします。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.HasSuffix("Amigo", "go")) fmt.Println(strings.HasSuffix("Amigo", "O")) fmt.Println(strings.HasSuffix("Amigo", "Ami")) fmt.Println(strings.HasSuffix("Amigo", "")) }
Output: true false false true
func Index ¶
Indexは、s内のsubstrの最初のインスタンスのインデックスを返します。substrがsに存在しない場合は-1を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.Index("chicken", "ken")) fmt.Println(strings.Index("chicken", "dmr")) }
Output: 4 -1
func IndexAny ¶
IndexAnyは、sにcharsの任意のUnicodeコードポイントの最初のインスタンスのインデックスを返します。 charsのUnicodeコードポイントがsに存在しない場合は-1を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.IndexAny("chicken", "aeiouy")) fmt.Println(strings.IndexAny("crwth", "aeiouy")) }
Output: 2 -1
func IndexByte ¶ added in v1.2.0
IndexByteは、s内の最初のcのインスタンスのインデックスを返します。 cがsに存在しない場合は-1を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.IndexByte("golang", 'g')) fmt.Println(strings.IndexByte("gophers", 'h')) fmt.Println(strings.IndexByte("golang", 'x')) }
Output: 0 3 -1
func IndexFunc ¶
IndexFuncは、f(c)がtrueを返す最初のUnicodeコードポイントのインデックスを返します。見つからない場合は-1を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" "github.com/shogo82148/std/unicode" ) func main() { f := func(c rune) bool { return unicode.Is(unicode.Han, c) } fmt.Println(strings.IndexFunc("Hello, 世界", f)) fmt.Println(strings.IndexFunc("Hello, world", f)) }
Output: 7 -1
func IndexRune ¶
IndexRuneは、Unicodeコードポイントrの最初のインスタンスのインデックスを返します。 rがsに存在しない場合は-1を返します。 rがutf8.RuneErrorの場合、無効なUTF-8バイトシーケンスの最初のインスタンスを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.IndexRune("chicken", 'k')) fmt.Println(strings.IndexRune("chicken", 'd')) }
Output: 4 -1
func Join ¶
Joinは、最初の引数の要素を連結して単一の文字列を作成します。区切り文字列sepは、結果の文字列の要素間に配置されます。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { s := []string{"foo", "bar", "baz"} fmt.Println(strings.Join(s, ", ")) }
Output: foo, bar, baz
func LastIndex ¶
LastIndexは、s内のsubstrの最後のインスタンスのインデックスを返します。 substrがs内に存在しない場合は-1を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.Index("go gopher", "go")) fmt.Println(strings.LastIndex("go gopher", "go")) fmt.Println(strings.LastIndex("go gopher", "rodent")) }
Output: 0 3 -1
func LastIndexAny ¶
LastIndexAnyは、sにcharsの任意のUnicodeコードポイントの最後のインスタンスのインデックスを返します。 charsのUnicodeコードポイントがsに存在しない場合は-1を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.LastIndexAny("go gopher", "go")) fmt.Println(strings.LastIndexAny("go gopher", "rodent")) fmt.Println(strings.LastIndexAny("go gopher", "fail")) }
Output: 4 8 -1
func LastIndexByte ¶ added in v1.5.0
LastIndexByteは、sの最後のインスタンスのインデックスを返します。 cがsに存在しない場合は-1を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.LastIndexByte("Hello, world", 'l')) fmt.Println(strings.LastIndexByte("Hello, world", 'o')) fmt.Println(strings.LastIndexByte("Hello, world", 'x')) }
Output: 10 8 -1
func LastIndexFunc ¶
LastIndexFuncは、f(c)がtrueを返す最後のUnicodeコードポイントのインデックスを返します。見つからない場合は-1を返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" "github.com/shogo82148/std/unicode" ) func main() { fmt.Println(strings.LastIndexFunc("go 123", unicode.IsNumber)) fmt.Println(strings.LastIndexFunc("123 go", unicode.IsNumber)) fmt.Println(strings.LastIndexFunc("go", unicode.IsNumber)) }
Output: 5 2 -1
func Map ¶
Mapは、マッピング関数に従ってすべての文字を変更した文字列sのコピーを返します。 マッピング関数が負の値を返す場合、文字は置換されずに文字列から削除されます。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { 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...")) }
Output: 'Gjnf oevyyvt naq gur fyvgul tbcure...
func Repeat ¶
Repeatは、文字列sのcount個のコピーからなる新しい文字列を返します。
countが負の場合、または(len(s) * count)の結果がオーバーフローする場合、パニックが発生します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println("ba" + strings.Repeat("na", 2)) }
Output: banana
func Replace ¶
Replaceは、古いものの最初のn個の重複しないインスタンスが新しいものに置き換えられた文字列sのコピーを返します。 oldが空の場合、文字列の先頭と各UTF-8シーケンスの後に一致し、kルーン文字列に対してk+1の置換が生成されます。 n < 0の場合、置換の数に制限はありません。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1)) }
Output: oinky oinky oink moo moo moo
func ReplaceAll ¶ added in v1.12.0
ReplaceAllは、古いもののすべての重複しないインスタンスが新しいものに置き換えられた文字列sのコピーを返します。 oldが空の場合、文字列の先頭と各UTF-8シーケンスの後に一致し、kルーン文字列に対してk+1の置換が生成されます。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.ReplaceAll("oink oink oink", "oink", "moo")) }
Output: moo moo moo
func Split ¶
Splitは、sをsepで区切り、それらの区切り文字の間の部分文字列のスライスを返します。
sがsepを含まず、sepが空でない場合、Splitは長さ1のスライスを返します。その唯一の要素はsです。
sepが空の場合、Splitは各UTF-8シーケンスの後に分割します。sとsepの両方が空の場合、Splitは空のスライスを返します。
countが-1のSplitNと同等です。
最初の区切り文字を基準に分割するには、Cutを参照してください。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Printf("%q\n", strings.Split("a,b,c", ",")) fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a ")) fmt.Printf("%q\n", strings.Split(" xyz ", "")) fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins")) }
Output: ["a" "b" "c"] ["" "man " "plan " "canal panama"] [" " "x" "y" "z" " "] [""]
func SplitAfter ¶
SplitAfterは、sをsepの後にスライスし、それらの部分文字列のスライスを返します。
sがsepを含まず、sepが空でない場合、SplitAfterは長さ1のスライスを返します。その唯一の要素はsです。
sepが空の場合、SplitAfterは各UTF-8シーケンスの後に分割します。sとsepの両方が空の場合、SplitAfterは空のスライスを返します。
countが-1のSplitAfterNと同等です。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Printf("%q\n", strings.SplitAfter("a,b,c", ",")) }
Output: ["a," "b," "c"]
func SplitAfterN ¶
SplitAfterNは、sをsepの後にスライスし、それらの部分文字列のスライスを返します。
countは、返す部分文字列の数を決定します。
n > 0:最大n個の部分文字列。最後の部分文字列は区切り文字以降の残りの部分です。 n == 0:結果はnil(部分文字列がゼロ個) n < 0:すべての部分文字列
sとsepのエッジケース(空の文字列など)は、SplitAfterのドキュメントで説明されているように処理されます。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Printf("%q\n", strings.SplitAfterN("a,b,c", ",", 2)) }
Output: ["a," "b,c"]
func SplitN ¶
SplitNは、sをsepで区切った部分文字列のスライスを返します。
countは、返す部分文字列の数を決定します。
n > 0:最大n個の部分文字列。最後の部分文字列は区切り文字以降の残りの部分です。 n == 0:結果はnil(部分文字列がゼロ個) n < 0:すべての部分文字列
sとsepのエッジケース(空の文字列など)は、Splitのドキュメントで説明されているように処理されます。
最初の区切り文字を基準に分割するには、Cutを参照してください。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Printf("%q\n", strings.SplitN("a,b,c", ",", 2)) z := strings.SplitN("a,b,c", ",", 0) fmt.Printf("%q (nil = %v)\n", z, z == nil) }
Output: ["a" "b,c"] [] (nil = true)
func Title
deprecated
Titleは、単語の先頭を表すすべてのUnicode文字をUnicodeタイトルケースにマップしたsのコピーを返します。
Deprecated: Titleが単語の境界に使用するルールは、Unicode句読点を適切に処理しません。代わりに、golang.org/x/text/casesを使用してください。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { // この例をToTitleの例と比較してください。 fmt.Println(strings.Title("her royal highness")) fmt.Println(strings.Title("loud noises")) fmt.Println(strings.Title("хлеб")) }
Output: Her Royal Highness Loud Noises Хлеб
func ToLower ¶
ToLowerは、すべてのUnicode文字を小文字にマップしたsを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.ToLower("Gopher")) }
Output: gopher
func ToLowerSpecial ¶
func ToLowerSpecial(c unicode.SpecialCase, s string) string
ToLowerSpecialは、Unicode文字をすべて、cで指定されたケースマッピングを使用して小文字にマップしたsのコピーを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" "github.com/shogo82148/std/unicode" ) func main() { fmt.Println(strings.ToLowerSpecial(unicode.TurkishCase, "Önnek İş")) }
Output: önnek iş
func ToTitle ¶
ToTitleは、すべてのUnicode文字をUnicodeタイトルケースにマップしたsのコピーを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { // この例をToTitleの例と比較してください。 fmt.Println(strings.ToTitle("her royal highness")) fmt.Println(strings.ToTitle("loud noises")) fmt.Println(strings.ToTitle("хлеб")) }
Output: HER ROYAL HIGHNESS LOUD NOISES ХЛЕБ
func ToTitleSpecial ¶
func ToTitleSpecial(c unicode.SpecialCase, s string) string
ToTitleSpecialは、Unicode文字をすべて、特別なケースルールに優先してUnicodeタイトルケースにマップしたsのコピーを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" "github.com/shogo82148/std/unicode" ) func main() { fmt.Println(strings.ToTitleSpecial(unicode.TurkishCase, "dünyanın ilk borsa yapısı Aizonai kabul edilir")) }
Output: DÜNYANIN İLK BORSA YAPISI AİZONAİ KABUL EDİLİR
func ToUpper ¶
ToUpperは、すべてのUnicode文字を大文字にマップしたsを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.ToUpper("Gopher")) }
Output: GOPHER
func ToUpperSpecial ¶
func ToUpperSpecial(c unicode.SpecialCase, s string) string
ToUpperSpecialは、Unicode文字をすべて、cで指定されたケースマッピングを使用して大文字にマップしたsのコピーを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" "github.com/shogo82148/std/unicode" ) func main() { fmt.Println(strings.ToUpperSpecial(unicode.TurkishCase, "örnek iş")) }
Output: ÖRNEK İŞ
func ToValidUTF8 ¶ added in v1.13.0
ToValidUTF8は、無効なUTF-8バイトシーケンスのランを置換文字列で置き換えたsのコピーを返します。置換文字列は空にすることができます。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Printf("%s\n", strings.ToValidUTF8("abc", "\uFFFD")) fmt.Printf("%s\n", strings.ToValidUTF8("a\xffb\xC0\xAFc\xff", "")) fmt.Printf("%s\n", strings.ToValidUTF8("\xed\xa0\x80", "abc")) }
Output: abc abc abc
func Trim ¶
Trimは、cutsetに含まれるすべての先頭と末尾のUnicodeコードポイントを削除した文字列sのスライスを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Print(strings.Trim("¡¡¡Hello, Gophers!!!", "!¡")) }
Output: Hello, Gophers
func TrimFunc ¶
TrimFuncは、f(c)がtrueを返す最初と最後のUnicodeコードポイントcを含まないように、文字列sの先頭と末尾からすべてのUnicodeコードポイントcを削除したスライスを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" "github.com/shogo82148/std/unicode" ) func main() { fmt.Print(strings.TrimFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })) }
Output: Hello, Gophers
func TrimLeft ¶
TrimLeftは、cutsetに含まれるすべての先頭のUnicodeコードポイントを削除した文字列sのスライスを返します。
接頭辞を削除するには、代わりにTrimPrefixを使用してください。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Print(strings.TrimLeft("¡¡¡Hello, Gophers!!!", "!¡")) }
Output: Hello, Gophers!!!
func TrimLeftFunc ¶
TrimLeftFuncは、f(c)がtrueを返す最初のUnicodeコードポイントcを含まないように、文字列sの先頭からすべてのUnicodeコードポイントcを削除したスライスを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" "github.com/shogo82148/std/unicode" ) func main() { fmt.Print(strings.TrimLeftFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })) }
Output: Hello, Gophers!!!
func TrimPrefix ¶ added in v1.1.0
TrimPrefixは、指定された接頭辞文字列を除いたsを返します。 sが接頭辞で始まらない場合、sは変更されずにそのまま返されます。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { var s = "¡¡¡Hello, Gophers!!!" s = strings.TrimPrefix(s, "¡¡¡Hello, ") s = strings.TrimPrefix(s, "¡¡¡Howdy, ") fmt.Print(s) }
Output: Gophers!!!
func TrimRight ¶
TrimRightは、cutsetに含まれるすべての末尾のUnicodeコードポイントを削除した文字列sのスライスを返します。
接尾辞を削除するには、代わりにTrimSuffixを使用してください。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Print(strings.TrimRight("¡¡¡Hello, Gophers!!!", "!¡")) }
Output: ¡¡¡Hello, Gophers
func TrimRightFunc ¶
TrimRightFuncは、f(c)がtrueを返す最後のUnicodeコードポイントcを含まないように、文字列sの末尾からすべてのUnicodeコードポイントcを削除したスライスを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" "github.com/shogo82148/std/unicode" ) func main() { fmt.Print(strings.TrimRightFunc("¡¡¡Hello, Gophers!!!", func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsNumber(r) })) }
Output: ¡¡¡Hello, Gophers
func TrimSpace ¶
TrimSpaceは、Unicodeで定義されるように、すべての先頭と末尾の空白を削除した文字列sのスライスを返します。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { fmt.Println(strings.TrimSpace(" \t\n Hello, Gophers \n\t\r\n")) }
Output: Hello, Gophers
func TrimSuffix ¶ added in v1.1.0
TrimSuffixは、指定された接尾辞文字列を除いたsを返します。 sが接尾辞で終わらない場合、sは変更されずにそのまま返されます。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { var s = "¡¡¡Hello, Gophers!!!" s = strings.TrimSuffix(s, ", Gophers!!!") s = strings.TrimSuffix(s, ", Marmots!!!") fmt.Print(s) }
Output: ¡¡¡Hello
Types ¶
type Builder ¶ added in v1.10.0
type Builder struct {
// contains filtered or unexported fields
}
Builderは、Writeメソッドを使用して効率的に文字列を構築するために使用されます。 メモリのコピーを最小限に抑えます。ゼロ値はすぐに使用できます。 非ゼロのBuilderをコピーしないでください。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { var b strings.Builder for i := 3; i >= 1; i-- { fmt.Fprintf(&b, "%d...", i) } b.WriteString("ignition") fmt.Println(b.String()) }
Output: 3...2...1...ignition
func (*Builder) Cap ¶ added in v1.12.0
Capは、ビルダーの基礎となるバイトスライスの容量を返します。 構築中の文字列に割り当てられた総スペースを含み、すでに書き込まれたバイトも含みます。
func (*Builder) Grow ¶ added in v1.10.0
Growは、必要に応じてbの容量を拡張し、別のnバイトのスペースを保証します。 Grow(n)の後、少なくともnバイトを別の割り当てなしでbに書き込むことができます。 nが負の場合、Growはパニックを引き起こします。
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Readerは、文字列から読み取りを行うことで、io.Reader、io.ReaderAt、io.ByteReader、io.ByteScanner、 io.RuneReader、io.RuneScanner、io.Seeker、およびio.WriterToインターフェースを実装します。 Readerのゼロ値は、空の文字列のReaderのように動作します。
func (*Reader) Size ¶ added in v1.5.0
Sizeは、基礎となる文字列の元の長さを返します。 Sizeは、ReadAtを介して読み取ることができるバイト数です。 返される値は常に同じであり、他のメソッドの呼び出しに影響を受けません。
func (*Reader) UnreadByte ¶
UnreadByteは、io.ByteScannerインターフェースを実装します。
func (*Reader) UnreadRune ¶
UnreadRuneは、io.RuneScannerインターフェースを実装します。
type Replacer ¶
type Replacer struct {
// contains filtered or unexported fields
}
Replacerは、置換物のリストで文字列を置換します。 複数のゴルーチンによる同時使用に対して安全です。
func NewReplacer ¶
NewReplacerは、古い文字列と新しい文字列のペアのリストから新しいReplacerを返します。 置換は、対象文字列に現れる順序で実行され、重複するマッチングは行われません。 古い文字列の比較は引数の順序で行われます。
NewReplacerは、奇数の引数が与えられた場合にパニックを引き起こします。
Example ¶
package main import ( "github.com/shogo82148/std/fmt" "github.com/shogo82148/std/strings" ) func main() { r := strings.NewReplacer("<", "<", ">", ">") fmt.Println(r.Replace("This is <b>HTML</b>!")) }
Output: This is <b>HTML</b>!