什么是runeGo?
我一直在谷歌搜索,但Golang只用一行说:rune是别名int32.
但是整个周围如何使用整数来交换案例?
以下是函数交换大小写.什么是<=和-?
为什么没有switch任何争论呢?
&&应该是说和,但什么是r <= 'z'?
func SwapRune(r rune) rune {
switch {
case 'a' <= r && r <= 'z':
return r - 'a' + 'A'
case 'A' <= r && r <= 'Z':
return r - 'A' + 'a'
default:
return r
}
}
Run Code Online (Sandbox Code Playgroud)
其中大多数来自http://play.golang.org/p/H6wjLZj6lW
func SwapCase(str string) string {
return strings.Map(SwapRune, str)
}
Run Code Online (Sandbox Code Playgroud)
我理解这是映射rune到string …
我想这样:
for i := 0; i < len(str); i++ {
dosomethingwithrune(str[i]) // takes a rune
}
Run Code Online (Sandbox Code Playgroud)
但事实证明,它str[i]有type byte(uint8)而不是rune.
如何用符文而不是字节迭代字符串?
我有以下代码,它应该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))
}
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我string用符文迭代一个符文,但实际上我需要int执行一些校验和计算.我真的需要来编码rune成一个[]byte,然后将其转换为一个string,然后用Atoi获得的int出来的rune?这是惯用的方式吗?
// The string `s` only contains digits.
var factor int
for i, c := range s[:12] {
if i % 2 == 0 {
factor = 1
} else {
factor = 3
}
buf := make([]byte, 1)
_ = utf8.EncodeRune(buf, c)
value, _ := strconv.Atoi(string(buf))
sum += value * factor
}
Run Code Online (Sandbox Code Playgroud)
我无法按字符排序字符串(检查两个字符串是否为字谜,我想对它们进行排序,并检查是否相等).
我可以得到这样[]rune的字符串表示s:
runes := make([]rune, len(s))
copy(runes, []rune(s))
Run Code Online (Sandbox Code Playgroud)
我可以像这样排序
someInts := []int{5, 2, 6, 3, 1, 4} // unsorted
sort.Ints(someInts)
Run Code Online (Sandbox Code Playgroud)
但这rune只是一个别名,int32所以我应该可以打电话
sort.Ints(runes)
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误:
cannot use runes (type []rune) as type []int in function argument
Run Code Online (Sandbox Code Playgroud)
那么......我如何对int32,int64或int*进行排序?
编辑:我确实得到了我的符文排序,但男孩,这是丑陋的.
type RuneSlice []rune
func (p RuneSlice) Len() int { return len(p) }
func (p RuneSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p RuneSlice) Swap(i, j int) { p[i], p[j] = …Run Code Online (Sandbox Code Playgroud) 根据https://blog.golang.org/strings和我的测试,看起来当我们range是一个字符串时,我们得到的字符是rune类型,但是如果我们通过 获取它str[index],它们将是byte类型,这是为什么呢?
这是我的代码片段:
\nvar converter = map[rune]rune {//some data}\n\nsample := "\xe2\x8c\x98\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf"\n\nvar tmp string\n\nfor _, runeValue := range sample {\n fmt.Printf("%+q", runeValue)\n tmp = fmt.Sprintf("%+q", runeValue)\n }\nRun Code Online (Sandbox Code Playgroud)\n的输出fmt.Printf("%+q", runeValue)是:
'\\u2318'\n'\\u3053'\n'\\u3093'\n'\\u306b'\n'\\u3061'\n'\\u306f'\nRun Code Online (Sandbox Code Playgroud)\n这些值实际上是符文,但由于 的返回类型Sprintf是字符串,我无法在我的地图中使用它,即[rune]rune。 \n我想知道如何将字符串转换为符文,或者换句话说,我该如何处理这个问题?
假设我有一个int64变量(或其他整数大小)表示一个有效的unicode代码点,并且我想将它转换为Go中的符文,我该怎么办?
在CI中会使用类似的类型:
c = (char) i; // 7 bit ascii only
Run Code Online (Sandbox Code Playgroud)
但是在Go中,类型断言不起作用:
c, err = rune.( i)
Run Code Online (Sandbox Code Playgroud)
建议?
我怎样才能知道字符串中子字符串的位置,用字符(或符文)代替字节?
strings.Index(s, sub) 将以字节为单位给出位置.使用Unicode时,它与符文中的位置不匹配:http://play.golang.org/p/DnlFjPaD2j
func main() {
s := "áéíóúÁÉÍÓÚ"
fmt.Println(strings.Index(s, "ÍÓ"))
}
Run Code Online (Sandbox Code Playgroud)
结果:14.预期:7
当然,我可以转换s,并sub以[]rune和寻找手动子切片,但有一个更好的方式来做到这一点?
与此相关,要获取n字符串的第一个字符,我正在这样做:string([]rune(s)[:n]).这是最好的方式吗?
我发现了以下特点:
b := "a"[0]
r := 'a'
fmt.Println(b == r) // Does not compile, cannot compare byte and rune
fmt.Println("a"[0] == 'a') // Compiles and prints "true"
Run Code Online (Sandbox Code Playgroud)
这是如何运作的?