golang语言渐入佳境[22]-string检索类函数

栏目: Go · 发布时间: 7年前

string检索类函数

package main

import (
	"fmt"
	"strings"
	"unicode"
)

/*
1、func Contains(s, substr string) bool
判断字符串s是否包含substr字符串

2、func ContainsAny(s, chars string) bool
判断字符串s是否包含chars字符串中的任一字符

3、func ContainsRune(s string, r rune) bool
判断字符串s是否包含unicode码值r

4、func Count(s, sep string) int
返回字符串s包含字符串sep的个数

5、func HasPrefix(s, prefix string) bool
判断字符串s是否有前缀字符串prefix

6、func HasSuffix(s, suffix string) bool
判断字符串s是否有后缀字符串suffix

7、func Index(s, sep string) int
返回字符串s中字符串sep首次出现的位置

8、func IndexAny(s, chars string) int
返回字符串chars中的任一unicode码值r在s中首次出现的位置

9、func IndexByte(s string, c byte) int
返回字符串s中字符c首次出现位置

10、func IndexFunc(s string, f func(rune) bool) int
返回字符串s中满足函数f(r)==true字符首次出现的位置

11、func IndexRune(s string, r rune) int
返回unicode码值r在字符串中首次出现的位置

12、func LastIndex(s, sep string) int
返回字符串s中字符串sep最后一次出现的位置

13、func LastIndexAny(s, chars string) int
返回字符串s中任意一个unicode码值r最后一次出现的位置

14、func LastIndexByte(s string, c byte) int
返回字符串s中字符c最后一次出现的位置

15、func LastIndexFunc(s string, f func(rune) bool) int
返回字符串s中满足函数f(r)==true字符最后一次出现的位置
 */
func main() {
	TestLastIndexFunc()
}

func TestContains() {
	fmt.Println(strings.Contains("seafood", "foo"))//true
	fmt.Println(strings.Contains("seafood", "bar"))//false
	fmt.Println(strings.Contains("seafood", ""))//true
	fmt.Println(strings.Contains("", ""))//true
	fmt.Println(strings.Contains("jonson郑2008", "郑"))//true
}

func TestContainsAny() {
	fmt.Println(strings.ContainsAny("team", "i"))//false
	fmt.Println(strings.ContainsAny("failure", "u & i"))//true
	fmt.Println(strings.ContainsAny("foo", ""))//false
	fmt.Println(strings.ContainsAny("", ""))//false
}

func TestContainsRune() {
	fmt.Println(strings.ContainsRune("一丁丂", '丁'))//true
	fmt.Println(strings.ContainsRune("一丁丂", 19969))//true
}

func TestCount() {
	fmt.Println(strings.Count("cheese", "e"))//3
	fmt.Println(strings.Count("one", ""))//4
}

func TestHasPrefix() {
	fmt.Println(strings.HasPrefix("1000phone news", "1000"))//true
	fmt.Println(strings.HasPrefix("1000phone news", "1000a"))//false
}

func TestHasSuffix() {
	fmt.Println(strings.HasSuffix("1000phone news", "news"))//true
	fmt.Println(strings.HasSuffix("1000phone news", "new"))//false
}

func TestIndex() {
	fmt.Println(strings.Index("chicken", "ken"))//4
	fmt.Println(strings.Index("chicken", "dmr"))//-1
}

func TestIndexAny() {
	fmt.Println(strings.IndexAny("abcABC120", "教育基地A"))//3
}

func TestIndexByte() {
	fmt.Println(strings.IndexByte("123abc", 'a'))//3
}

func TestIndexRune() {
	fmt.Println(strings.IndexRune("abcABC120", 'C'))//5
	fmt.Println(strings.IndexRune("It培训教育", '教'))//8
}

func TestIndexFunc() {
	f := func(c rune) bool {
		return unicode.Is(unicode.Han , c)
	}
	fmt.Println(strings.IndexFunc("Hello123,中国" , f))//9
}

func TestLastIndex() {
	fmt.Println(strings.LastIndex("jonson learn english", "e"))//13
	fmt.Println(strings.Index("go gopher", "go"))//0
	fmt.Println(strings.LastIndex("go gopher", "go"))//3
	fmt.Println(strings.LastIndex("go gopher", "rodent"))//-1
}

func TestLastIndexAny() {
	fmt.Println(strings.LastIndexAny("chicken", "aeiouy"))//5
	fmt.Println(strings.LastIndexAny("crwth", "aeiouy"))//-1
}

func TestLastIndexByte() {
	fmt.Println(strings.LastIndexByte("abcABCA123", 'A'))//6
}


func TestLastIndexFunc() {
	f := func(c rune) bool {
		return unicode.Is(unicode.Han, c)
	}
	fmt.Println(strings.LastIndexFunc("Hello,世界", f))//9
	fmt.Println(strings.LastIndexFunc("Hello,world中国人", f))//17
}

golang语言渐入佳境[22]-string检索类函数


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

引爆点

引爆点

[美] 马尔科姆·格拉德威尔 / 钱清、覃爱冬 / 中信出版社 / 2009-8 / 27.00元

我们的世界看上去很坚固,但在《纽约客》怪才格拉德威尔的眼里,只要你找到那个点,轻轻一触,这个世界就会动起来:一位满意而归的顾客能让新开张的餐馆座无虚席,一位涂鸦爱好者能在地铁掀起犯罪浪潮,一位精明小伙传递的信息拉开了美国独立战争的序幕——这个看起来不起眼的点,却是任何人都不能忽视的引爆点。 《引爆点》是一本谈论怎样让产品发起流行潮的专门性著作。书中将产品爆发流行的现象归因为三种模式:个别人物......一起来看看 《引爆点》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具