栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

对于字符串切片的循环迭代不起作用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

对于字符串切片的循环迭代不起作用

软件包bufio

func(* Reader)ReadString

func (b *Reader) ReadString(delim byte) (string, error)

ReadString读取直到输入中第一次出现delim为止,并返回一个字符串,其中包含直到定界符(包括定界符)的数据。如果ReadString在找到定界符之前遇到错误,它将返回错误之前读取的数据和错误本身(通常为io.EOF)。当且仅当返回的数据未以delim结尾时,ReadString才返回err!=
nil。


返回一个字符串,该字符串包含直到定界符(包括定界符)的数据。

删除任何结尾的换行符:

"n"
"rn"
。为了快速解决,请写:

sentence := strings.Split(strings.TrimSpace(sentenceraw), " ")

例如,

package mainimport (    "bufio"    "fmt"    "os"    "regexp"    "strings"    "github.com/stretchr/stew/slice")func main() {    lst := []string{"sh", "gl", "ch", "ph", "tr", "br", "fr", "bl", "gr", "st", "sl", "cl", "pl", "fl", "th"}    reader := bufio.NewReader(os.Stdin)    fmt.Print("Type what you would like translated into pig-latin and press ENTER: ")    sentenceraw, _ := reader.ReadString('n')    sentence := strings.Split(strings.TrimSpace(sentenceraw), " ")    isAlpha := regexp.MustCompile(`^[A-Za-z]+$`).MatchString    newsentence := make([]string, 0)    for _, i := range sentence {        if slice.Contains([]byte{'a', 'e', 'i', 'o', 'u'}, i[0]) { newsentence = append(newsentence, strings.Join([]string{string(i), "ay"}, ""))        } else if slice.Contains(lst, string(i[0])+string(i[1])) { newsentence = append(newsentence, strings.Join([]string{string(i[2:]), string(i[:2]), "ay"}, ""))        } else if !isAlpha(string(i)) { newsentence = append(newsentence, strings.Join([]string{string(i)}, ""))        } else { newsentence = append(newsentence, strings.Join([]string{string(i[1:]), string(i[0]), "ay"}, ""))        }    }    fmt.Println(strings.Join(newsentence, " "))}

输出:

Type what you would like translated into pig-latin and press ENTER: hello worldellohay orldway


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/448017.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号