• go_字符和字符串处理


    rune相当于go的char

    使用range遍历pos,rune对

    使用utf8.RuneCountInString(s)获得字符数量

    使用len获得字节长度,使用[]byte获得字节

    一般把字节转成[]rune,更加容易操作

    package main
    
    import (
    	"fmt"
    	"unicode/utf8"
    )
    
    func main() {
    	s:="Yes我爱上百度!"
    	fmt.Println(s)
    	for _,b:=range []byte(s)  {
    		fmt.Printf("%X ",b)
    	}
    	fmt.Println()
    
    	for i,ch:=range s{//ch is  a rune
    		fmt.Printf("(%d %X)",i,ch)
    	}
    	fmt.Println()
    	fmt.Println("Rune 长度",
    			utf8.RuneCountInString(s))
    
    	bytes :=[]byte(s)
    	for len(bytes)>0{
    		ch,size:=utf8.DecodeRune(bytes)
    		bytes = bytes[size:]
    		fmt.Printf("%c ",ch)
    	}
    	fmt.Println()
    
    	for i,ch :=range []rune(s){
    		fmt.Printf("(%d %c)",i,ch)
    	}
    	fmt.Println()
    }
    

      

     

  • 相关阅读:
    *Integer to English Words
    *Palindrome Linked List
    *Partition List
    Sort Colors
    wireshark tls
    find 路径必须在表达式之前
    http://mozilla.debian.net/
    maven bundle
    xmpp
    xmlns=""
  • 原文地址:https://www.cnblogs.com/luffe/p/8546921.html
Copyright © 2020-2023  润新知