您的位置:首页 > 编程语言 > Go语言

go提取字符串中的中文并生成字符串

2016-09-11 00:14 176 查看
package main

import (
"fmt"
//	"time"
)

//19968  40869
func main() {
str := "1撒zxz是谁我我说-22_-laoYu#$@sd兰考县"
r := []rune(str)
//fmt.Println("rune=", r)
strSlice := []string{}
cnstr := ""
for i := 0; i < len(r); i++ {
if r[i] <= 40869 && r[i] >= 19968 {
cnstr = cnstr + string(r[i])
strSlice = append(strSlice, cnstr)

}
//fmt.Println("r[", i, "]=", r[i], "string=", string(r[i]))
}
if 0 == len(strSlice) {
//无中文,需要跳过,后面再找规律
}
fmt.Println("原字符串:", str, "    提取出的中文字符串:", cnstr)
fmt.Println(strSlice)

}
<p style="margin-top: 0px; margin-bottom: 0px;"><!--StartFragment--><span style=" color:#ffffff; background-color:#272822;">原字符串: 1撒zxz是谁我我说-22_-laoYu#$@sd兰考县     提取出的中文字符串: 撒是谁我我说兰考县</span></p><p style="margin-top: 0px; margin-bottom: 0px;"><span style=" color:#ffffff; background-color:#272822;">[撒 撒是 撒是谁 撒是谁我 撒是谁我我 撒是谁我我说 撒是谁我我说兰 撒是谁我我说兰考 撒是谁我我说兰考县]</span><!--EndFragment--></p>

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  go