Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
71c8b82d
Commit
71c8b82d
authored
Feb 13, 2012
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
strings: more examples
R=golang-dev, adg CC=golang-dev
https://golang.org/cl/5645092
parent
aee1c38c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
155 additions
and
0 deletions
+155
-0
example_test.go
src/pkg/strings/example_test.go
+155
-0
No files found.
src/pkg/strings/example_test.go
View file @
71c8b82d
...
@@ -13,3 +13,158 @@ import (
...
@@ -13,3 +13,158 @@ import (
func
ExampleFields
()
{
func
ExampleFields
()
{
fmt
.
Printf
(
"Fields are: %q"
,
strings
.
Fields
(
" foo bar baz "
))
fmt
.
Printf
(
"Fields are: %q"
,
strings
.
Fields
(
" foo bar baz "
))
}
}
// true
// false
// true
// true
func
ExampleContains
()
{
fmt
.
Println
(
strings
.
Contains
(
"seafood"
,
"foo"
))
fmt
.
Println
(
strings
.
Contains
(
"seafood"
,
"bar"
))
fmt
.
Println
(
strings
.
Contains
(
"seafood"
,
""
))
fmt
.
Println
(
strings
.
Contains
(
""
,
""
))
}
// false
// true
// false
// false
func
ExampleContainsAny
()
{
fmt
.
Println
(
strings
.
ContainsAny
(
"team"
,
"i"
))
fmt
.
Println
(
strings
.
ContainsAny
(
"failure"
,
"u & i"
))
fmt
.
Println
(
strings
.
ContainsAny
(
"foo"
,
""
))
fmt
.
Println
(
strings
.
ContainsAny
(
""
,
""
))
}
// 3
// 5
func
ExampleCount
()
{
fmt
.
Println
(
strings
.
Count
(
"cheese"
,
"e"
))
fmt
.
Println
(
strings
.
Count
(
"five"
,
""
))
// before & after each rune
}
// true
func
ExampleEqualFold
()
{
fmt
.
Println
(
strings
.
EqualFold
(
"Go"
,
"go"
))
}
// 4
// -1
func
ExampleIndex
()
{
fmt
.
Println
(
strings
.
Index
(
"chicken"
,
"ken"
))
fmt
.
Println
(
strings
.
Index
(
"chicken"
,
"dmr"
))
}
// 4
// -1
func
ExampleRune
()
{
fmt
.
Println
(
strings
.
IndexRune
(
"chicken"
,
'k'
))
fmt
.
Println
(
strings
.
IndexRune
(
"chicken"
,
'd'
))
}
// 0
// 3
// -1
func
ExampleLastIndex
()
{
fmt
.
Println
(
strings
.
Index
(
"go gopher"
,
"go"
))
fmt
.
Println
(
strings
.
LastIndex
(
"go gopher"
,
"go"
))
fmt
.
Println
(
strings
.
LastIndex
(
"go gopher"
,
"rodent"
))
}
// foo, bar, baz
func
ExampleJoin
()
{
s
:=
[]
string
{
"foo"
,
"bar"
,
"baz"
}
fmt
.
Println
(
strings
.
Join
(
s
,
", "
))
}
// banana
func
ExampleRepeat
()
{
fmt
.
Println
(
"ba"
+
strings
.
Repeat
(
"na"
,
2
))
}
// oinky oinky oink
// moo moo moo
func
ExampleReplace
()
{
fmt
.
Println
(
strings
.
Replace
(
"oink oink oink"
,
"k"
,
"ky"
,
2
))
fmt
.
Println
(
strings
.
Replace
(
"oink oink oink"
,
"oink"
,
"moo"
,
-
1
))
}
// ["a" "b" "c"]
// ["" "man " "plan " "canal panama"]
// [" " "x" "y" "z" " "]
func
ExampleSplit
()
{
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 "
,
""
))
}
// ["a" "b,c"]
// [] (nil = true)
func
ExampleSplitN
()
{
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
)
}
// ["a," "b," "c"]
func
ExampleSplitAfter
()
{
fmt
.
Printf
(
"%q
\n
"
,
strings
.
SplitAfter
(
"a,b,c"
,
","
))
}
// ["a," "b,c"]
func
ExampleSplitAfterN
()
{
fmt
.
Printf
(
"%q
\n
"
,
strings
.
SplitAfterN
(
"a,b,c"
,
","
,
2
))
}
// Her Royal Highness
func
ExampleTitle
()
{
fmt
.
Println
(
strings
.
Title
(
"her royal highness"
))
}
// LOUD NOISES
// ХЛЕБ
func
ExampleToTitle
()
{
fmt
.
Println
(
strings
.
ToTitle
(
"loud noises"
))
fmt
.
Println
(
strings
.
ToTitle
(
"хлеб"
))
}
// [Achtung]
func
ExampleTrim
()
{
fmt
.
Printf
(
"[%s]"
,
strings
.
Trim
(
" !!! Achtung !!! "
,
"! "
))
}
// 'Gjnf oevyyvt naq gur fyvgul tbcure...
func
ExampleMap
()
{
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..."
))
}
// a lone gopher
func
ExampleTrimSpace
()
{
fmt
.
Println
(
strings
.
TrimSpace
(
"
\t\n
a lone gopher
\n\t\r\n
"
))
}
// This is <b>HTML</b>!
func
ExampleNewReplacer
()
{
r
:=
strings
.
NewReplacer
(
"<"
,
"<"
,
">"
,
">"
)
fmt
.
Println
(
r
.
Replace
(
"This is <b>HTML</b>!"
))
}
// GOPHER
func
ExampleToUpper
()
{
fmt
.
Println
(
strings
.
ToUpper
(
"Gopher"
))
}
// gopher
func
ExampleToLower
()
{
fmt
.
Println
(
strings
.
ToLower
(
"Gopher"
))
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment