如何获取Golang字符串的最后X个字符? [英] How to get the last X Characters of a Golang String?
本文介绍了如何获取Golang字符串的最后X个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有字符串 12121211122,并且想获取最后3个字符(例如 122),那么Go中可以吗?我看过 string
包,没有看到类似 getLastXcharacters
的东西。
If I have the string "12121211122" and I want to get the last 3 characters (e.g. "122"), is that possible in Go? I've looked in the string
package and didn't see anything like getLastXcharacters
.
推荐答案
您可以在a上使用切片表达式字符串以获取最后三个字节。
You can use a slice expression on a string to get the last three bytes.
s := "12121211122"
first3 := s[0:3]
last3 := s[len(s)-3:]
或者如果您正在使用unicode,您可以执行以下操作:
Or if you're using unicode you can do something like:
s := []rune("世界世界世界")
first3 := string(s[0:3])
last3 := string(s[len(s)-3:])
检查 Go中的字符串,字节,符文和字符和切片技巧。
这篇关于如何获取Golang字符串的最后X个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文