當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang strings.IndexByte()用法及代碼示例

strings.IndexByte() Golang中的函數返回原始字符串中給定字節的第一個實例的索引。如果給定的字節在原始字符串中不可用,則此方法將返回-1。

用法:

func IndexByte(str string, b byte) int

在這裏,str是原始字符串,b是一個字節,我們要查找其索引值。讓我們借助示例來討論這個概念:

範例1:

// Golang program to illustrate the 
// strings.IndexByte() Function 
package main 
  
// importing fmt and strings 
import ( 
    "fmt"
    "strings"
) 
  
// calling main method 
func main() { 
  
    // 'g' present at index 0 of string. 
    fmt.Println(strings.IndexByte("geeksforgeeks", 'g')) 
      
    // 's' present at index 8 of string. 
    fmt.Println(strings.IndexByte("computerscience", 's')) 
}

輸出:

0
8

範例2:

// Golang program to illustrate the 
// strings.IndexByte() Function 
package main 
  
// importing fmt and strings 
import ( 
    "fmt"
    "strings"
) 
  
// calling main method 
func main() { 
  
    // 's' not present in string. 
    fmt.Println(strings.IndexByte("computerscience", 'S')) 
    // 'f' not present in string. 
    fmt.Println(strings.IndexByte("GFG", 'f')) 
}

輸出:

-1
-1



相關用法


注:本文由純淨天空篩選整理自PranchalKatiyar大神的英文原創作品 strings.IndexByte() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。