Skip to content

Commit

Permalink
Update the work of today
Browse files Browse the repository at this point in the history
FieldsFunc, HasPrefix, HasSuffix, Index, IndexAny
  • Loading branch information
jemygraw committed Nov 16, 2013
1 parent 1f10958 commit c0abdd8
Showing 1 changed file with 84 additions and 1 deletion.
85 changes: 84 additions & 1 deletion Go_Lib/Go语言包之 strings.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,87 @@ s2=" "
fmt.Println(strings.Fields(s))
//[we are a team we can fight together]
//[]
```
```

**FieldsFunc**(s string, f func(rune) bool) []string
这个方法判断字符串s中的每个Unicode字符,如果发现用c作为函数f的参数能够让f返回true的话,则用这个字符c来分割字符串s。并返回分割后的字符串数组。如果s字符串中所有的字符都可以让函数f返回true,或者字符串s为空,那么返回一个空字符串数组。
```go
package main
import (
"fmt"
"strings"
)
func is_a_or_b(c rune) bool{
result:=false
if c>='a' && c <='b'{
result=true
}
return result
}
func is_我(c rune) bool{
result:=false
if c == ''{
result=true
}
return result
}
func main(){
s:="we are a team 我是大猪猪 "
r_array:=strings.FieldsFunc(s, is_a_or_b)
for i:=0; i<len(r_array);i++{
fmt.Println("["+r_array[i]+"]")
}
fmt.Println("---------------------------")
x_array:=strings.FieldsFunc(s,is_我)
for i:=0; i<len(x_array);i++{
fmt.Println("["+x_array[i]+"]")
}
}
/*
[we ]
[re ]
[ te]
[m 我是大猪猪 ]
---------------------------
[we are a team ]
[是大猪猪 ]
*/
```

**HasPrefix**(s, prefix string) bool
这个方法检查s是否以字符串prefix开头。
```go
s:="we are a team 我是大猪猪 "
result:=strings.HasPrefix(s, "we")
fmt.Println(result)//true
```

**HasSuffix**(s, suffix string) bool
这个方法检查s是否以字符串suffix结尾
```go
s:="we are a team 我是大猪猪"
result:=strings.HasSuffix(s, "大猪猪")
fmt.Println(result) //true
```

**Index**(s, sep string) int
这个方法返回字符串sep在s中第一次出现的位置索引,如果s中不存在sep,那么返回-1。
```go
s:="we are a team , who are you"
fmt.Println(strings.Index(s, "are"))//3
fmt.Println(strings.Index(s, "and"))//-1
```
**IndexAny**(s, chars string) int
这个方法依次按Unicode字符检查chars字符串中的字符是否存在于s中,如果存在则返回s字符串中第一次出现chars中字符的位置索引,如果chars里面的字符都不存在于s中,那么返回-1。
```go
s:="we are a team , who are you"
chars:="help"
fmt.Println(strings.IndexAny(s, chars))//1
chars="x soilder"
fmt.Println(strings.IndexAny(s, chars))//1,亲空格也是字符哦
chars="go"
fmt.Println(strings.IndexAny(s, chars))//18
chars="silk"
fmt.Println(strings.IndexAny(s, chars))//-1
```

0 comments on commit c0abdd8

Please sign in to comment.