Categories: GoFrame 教程

GoFrame gstr-位置查找

Pos

  • 说明:​Pos​返回​needle​在haystack中第一次出现的位置,区分大小写。 如果没有找到,则返回-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

  • 说明:​PosRune​的作用于函数​Pos​相似,但支持​haystack​和​needle​为​unicode​字符串。
  • 格式:
PosRune(haystack, needle string, startOffset ...int) int
  • 示例:
func ExamplePosRune() {
 var (
  haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
  needle   = `Go`
  posI     = gstr.PosRune(haystack, needle)
  posR     = gstr.PosRRune(haystack, needle)
 )
 fmt.Println(posI)
 fmt.Println(posR)

 // Output:
 // 0
 // 22
}

PosI

  • 说明:​PosI​返回​needle​在​haystack​中第一次出现的位置,不区分大小写。 如果没有找到,则返回​-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

  • 说明:​PosRuneI​的作用于函数​PosI​相似,但支持​haystack​和​needle​为​unicode​字符串。
  • 格式:
PosIRune(haystack, needle string, startOffset ...int) int
  • 示例:
func ExamplePosIRune() {
 {
  var (
   haystack    = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
   needle      = `高性能`
   startOffset = 10
   result      = gstr.PosIRune(haystack, needle, startOffset)
  )
  fmt.Println(result)
 }
 {
  var (
   haystack    = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
   needle      = `高性能`
   startOffset = 30
   result      = gstr.PosIRune(haystack, needle, startOffset)
  )
  fmt.Println(result)
 }

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

PosR

  • 说明:​PosR​返回​needle​在​haystack​中最后一次出现的位置,区分大小写。 如果没有找到,则返回​-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

  • 说明:​PosRuneR​的作用于函数​PosR​相似,但支持​haystack​和​needle​为​unicode​字符串。
  • 格式:
PosRRune(haystack, needle string, startOffset ...int) int
  • 示例:
func ExamplePosRRune() {
 var (
  haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
  needle   = `Go`
  posI     = gstr.PosIRune(haystack, needle)
  posR     = gstr.PosRRune(haystack, needle)
 )
 fmt.Println(posI)
 fmt.Println(posR)

 // Output:
 // 0
 // 22
}

PosRI

  • 说明:​PosRI​返回​needle​在​haystack​中最后一次出现的位置,不区分大小写。 如果没有找到,则返回​-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

  • 说明:​PosRIRune​的作用于函数​PosRI​相似,但支持​haystack​和​needle​为​unicode​字符串。
  • 格式:
PosRIRune(haystack, needle string, startOffset ...int) int
  • 示例:
func ExamplePosRIRune() {
 var (
  haystack = `GoFrame是一款模块化、高性能、企业级的Go基础开发框架`
  needle   = `GO`
  posI     = gstr.PosIRune(haystack, needle)
  posR     = gstr.PosRIRune(haystack, needle)
 )
 fmt.Println(posI)
 fmt.Println(posR)

 // Output:
 // 0
 // 22
}

冒牌SEO

前端开发者,欢迎大家一起沟通和交流。

Share
Published by
冒牌SEO

Recent Posts

聊聊vue3中的defineProps

在Vue 3中,defineP…

1 周 ago

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 周 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

3 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

4 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

1 月 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

1 月 ago