Golang从符文转换为字符串

njthzxwz  于 2023-10-14  发布在  Go
关注(0)|答案(6)|浏览(131)

我有下面的代码,它应该将rune转换为string并打印出来。然而,我得到未定义的字符时,它是打印。我不知道bug在哪里:

package main

import (
    "fmt"
    "strconv"
    "strings"
    "text/scanner"
)

func main() {
    var b scanner.Scanner
    const a = `a`
    b.Init(strings.NewReader(a))
    c := b.Scan()
    fmt.Println(strconv.QuoteRune(c))
}
unhi4e5o

unhi4e5o1#

这是因为你使用Scanner.Scan()来读取rune,但它做了别的事情。Scanner.Scan()可以用来读取由Scanner.Mode位掩码控制的 tokensrune s特殊tokens,它返回text/scanner包中的特殊常量,而不是读取的rune本身。
要读取单个rune,请使用Scanner.Next()

c := b.Next()
fmt.Println(c, string(c), strconv.QuoteRune(c))

输出量:

97 a 'a'

如果您只想将单个rune转换为string,请使用简单的类型转换。runeint32的别名,将整数转换为string
将有符号或无符号整数值转换为字符串类型会生成一个包含该整数的UTF-8表示形式的字符串。
所以:

r := rune('a')
fmt.Println(r, string(r))

输出:

97 a

同样,要在string值的字符串上循环,您可以简单地使用for ... range结构:

for i, r := range "abc" {
    fmt.Printf("%d - %c (%v)\n", i, r, r)
}

输出量:

0 - a (97)
1 - b (98)
2 - c (99)

或者,您可以简单地将string值转换为[]rune

fmt.Println([]rune("abc")) // Output: [97 98 99]

还有utf8.DecodeRuneInString()
Go Playground上尝试示例。

注:

您的原始代码(使用Scanner.Scan())如下所示:
1.调用Scanner.Init(),将Mode(b.Mode)设置为scanner.GoTokens
1.在输入上调用Scanner.Scan()(从"a")返回scanner.Ident,因为"a"是一个有效的Go标识符:

c := b.Scan()
if c == scanner.Ident {
    fmt.Println("Identifier:", b.TokenText())
}

// Output: "Identifier: a"
7nbnzgx9

7nbnzgx92#

我知道我有点迟到了,但这里有一个[]rune to string函数:

func runesToString(runes []rune) (outString string) {
    // don't need index so _
    for _, v := range runes {
        outString += string(v)
    }
    return
}

是的,有一个命名的return,但我认为它在这种情况下是好的,因为它减少了行数,函数只是短

7fhtutme

7fhtutme3#

这段简单的代码可以将一个符文转换为字符串

s := fmt.Sprintf("%c", rune)
mf98qq94

mf98qq944#

自从我来到这个问题搜索符文和字符串和字符,认为这可能会帮助像我这样的新手

// str := "aഐbc"
// testString(str)
func testString(oneString string){

    //string to byte slice - No sweat -just type cast it
    // As string  IS A byte slice
    var twoByteArr []byte = []byte(oneString)

    // string to rune Slices - No sweat 
    // string IS A slice of runes 
    var threeRuneSlice []rune = []rune(oneString)

   // Hmm! String seems to have a dual personality it is both a slice of bytes and
   // a slice of runes - yeah - read on
    
    // A rune slice can be convered to string -
    // No sweat - as string == rune slice
    var thrirdString string = string(threeRuneSlice)
    
    // There is a catch here and that is in printing "characters", using for loop and range 
    
    fmt.Println("Chars in oneString")
    for i,r := range oneString {
        fmt.Printf(" %d  %v  %c ",i,r,r) //you may not get index 0,1,2,3 here  
        // since the range runs specially over strings  https://blog.golang.org/strings
    }
    
    fmt.Println("\nChars in threeRuneSlice")
    for i,r := range threeRuneSlice {
        fmt.Printf(" %d  %v  %c ",i,r,r) // i = 0,1,2,4 , perfect!!
        // as runes are made up of 4 bytes (rune is int32 and byte in unint8
        // and a set of bytes is used to represent a rune which is used to 
       // represent  UTF characters == the REAL CHARECTER 
    }

    fmt.Println("\nValues in oneString ")
    for j := 0; j < len(oneString); j++ {
        fmt.Printf(" %d %v ",j,oneString[j]) // No you cannot get charecters if you iterate through string in this way
        // as you are going over bytes here - not runes
    }
    fmt.Println("\nValues in twoByteArr")
    for j := 0; j < len(twoByteArr); j++ {
        fmt.Printf(" %d=%v ",j,twoByteArr[j]) // == same as above
    }
    
    fmt.Printf("\none - %s, two %s, three %s\n",oneString,twoByteArr,thrirdString)
}

还有一些毫无意义的演示https://play.golang.org/p/tagRBVG8k7V改编自https://groups.google.com/g/golang-nuts/c/84GCvDBhpbg/m/Tt6089MPFQAJ
以显示“字符”根据unicode代码点用1到4个字节编码

r6hnlfcb

r6hnlfcb5#

可以使用字符串函数

string(a)

Go playground

8iwquhpp

8iwquhpp6#

下面是一个简单的例子:

// rune => string
fmt.Printf("%c\n", 65) // A
fmt.Println(string(rune(0x1F60A))) // 😊
fmt.Println(string([]rune{0x1F468, 0x200D, 0x1F9B0})) // 👨‍🦰

// string => rune
fmt.Println(strconv.FormatUint(uint64([]rune("😊")[0]), 16)) // 1f60a
fmt.Printf("%U\n", '😊') // U+1F60A
fmt.Printf("%U %U %U\n", '👨', '‍', '🦰') // U+1F468 U+200D U+1F9B0

go playground

相关问题