GoFrame gstr-位置查找

2022-04-11 17:19 更新

Pos

  • 說(shuō)明:?Pos?返回?needle?在haystack中第一次出現(xiàn)的位置,區(qū)分大小寫(xiě)。 如果沒(méi)有找到,則返回-1。  
  • 格式:

Pos(haystack, needle string, startOffset ...int) int

  • 示例:

func ExamplePos() {
	var (
		haystack = `Hello World`
		needle   = `World`
		result   = gstr.Pos(haystack, needle)
	)
	fmt.Println(result)

	// Output:
	// 6
}

PosRune

  • 說(shuō)明:?PosRune?的作用于函數(shù)?Pos?相似,但支持?haystack?和?needle?為?unicode?字符串。  
  • 格式:

PosRune(haystack, needle string, startOffset ...int) int

  • 示例:

func ExamplePosRune() {
	var (
		haystack = `GoFrame是一款模塊化、高性能、企業(yè)級(jí)的Go基礎(chǔ)開(kāi)發(fā)框架`
		needle   = `Go`
		posI     = gstr.PosRune(haystack, needle)
		posR     = gstr.PosRRune(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

	// Output:
	// 0
	// 22
}

PosI

  • 說(shuō)明:?PosI?返回?needle?在?haystack?中第一次出現(xiàn)的位置,不區(qū)分大小寫(xiě)。 如果沒(méi)有找到,則返回?-1?。  
  • 格式:

PosI(haystack, needle string, startOffset ...int) int

  • 示例:

func ExamplePosI() {
	var (
		haystack = `goframe is very, very easy to use`
		needle   = `very`
		posI     = gstr.PosI(haystack, needle)
		posR     = gstr.PosR(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

	// Output:
	// 11
	// 17
}

PosRuneI

  • 說(shuō)明:?PosRuneI?的作用于函數(shù)?PosI?相似,但支持?haystack?和?needle?為?unicode?字符串。  
  • 格式:

PosIRune(haystack, needle string, startOffset ...int) int

  • 示例:

func ExamplePosIRune() {
	{
		var (
			haystack    = `GoFrame是一款模塊化、高性能、企業(yè)級(jí)的Go基礎(chǔ)開(kāi)發(fā)框架`
			needle      = `高性能`
			startOffset = 10
			result      = gstr.PosIRune(haystack, needle, startOffset)
		)
		fmt.Println(result)
	}
	{
		var (
			haystack    = `GoFrame是一款模塊化、高性能、企業(yè)級(jí)的Go基礎(chǔ)開(kāi)發(fā)框架`
			needle      = `高性能`
			startOffset = 30
			result      = gstr.PosIRune(haystack, needle, startOffset)
		)
		fmt.Println(result)
	}

	// Output:
	// 14
	// -1
}

PosR

  • 說(shuō)明:?PosR?返回?needle?在?haystack?中最后一次出現(xiàn)的位置,區(qū)分大小寫(xiě)。 如果沒(méi)有找到,則返回?-1?。  
  • 格式:

PosR(haystack, needle string, startOffset ...int) int

  • 示例:

func ExamplePosR() {
	var (
		haystack = `goframe is very, very easy to use`
		needle   = `very`
		posI     = gstr.PosI(haystack, needle)
		posR     = gstr.PosR(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

	// Output:
	// 11
	// 17
}

PosRuneR

  • 說(shuō)明:?PosRuneR?的作用于函數(shù)?PosR?相似,但支持?haystack?和?needle?為?unicode?字符串。  
  • 格式:

PosRRune(haystack, needle string, startOffset ...int) int

  • 示例:

func ExamplePosRRune() {
	var (
		haystack = `GoFrame是一款模塊化、高性能、企業(yè)級(jí)的Go基礎(chǔ)開(kāi)發(fā)框架`
		needle   = `Go`
		posI     = gstr.PosIRune(haystack, needle)
		posR     = gstr.PosRRune(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

	// Output:
	// 0
	// 22
}

PosRI

  • 說(shuō)明:?PosRI?返回?needle?在?haystack?中最后一次出現(xiàn)的位置,不區(qū)分大小寫(xiě)。 如果沒(méi)有找到,則返回?-1?。  
  • 格式:

PosRI(haystack, needle string, startOffset ...int) int

  • 示例:

func ExamplePosRI() {
	var (
		haystack = `goframe is very, very easy to use`
		needle   = `VERY`
		posI     = gstr.PosI(haystack, needle)
		posR     = gstr.PosRI(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

	// Output:
	// 11
	// 17
}

PosRIRune

  • 說(shuō)明:?PosRIRune?的作用于函數(shù)?PosRI?相似,但支持?haystack?和?needle?為?unicode?字符串。  
  • 格式:

PosRIRune(haystack, needle string, startOffset ...int) int

  • 示例:

func ExamplePosRIRune() {
	var (
		haystack = `GoFrame是一款模塊化、高性能、企業(yè)級(jí)的Go基礎(chǔ)開(kāi)發(fā)框架`
		needle   = `GO`
		posI     = gstr.PosIRune(haystack, needle)
		posR     = gstr.PosRIRune(haystack, needle)
	)
	fmt.Println(posI)
	fmt.Println(posR)

	// Output:
	// 0
	// 22
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)