登录
首页 >  Golang >  Go问答

字符串切片上的 For 循环迭代不起作用

来源:Golang技术栈

时间:2023-04-11 19:32:48 353浏览 收藏

小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《字符串切片上的 For 循环迭代不起作用》,以下内容将会涉及到golang,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

问题内容

我写了这段代码,它应该将一个小写的英语短语翻译成猪拉丁语。

package main

import (
    "fmt"
    "strings"
    "bufio"
    "github.com/stretchr/stew/slice"
    "regexp"
    "os"
)

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(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, " "))
}

但是,它对短语中的最后一个词没有做任何事情。

如果我使用“the quick brown fox jumped over the lazy dog”这个句子,我会得到“ethay uickqay ownbray oxfay umpedjay overay ethay azylay dog”

这里的一切都是对的,除了最后一句话!为什么它不起作用?如果我使用“hello world”作为我的短语,也会发生同样的事情。

正确答案

包 bufio

func (*Reader) 读取字符串

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

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


返回一个字符串,其中包含直到并包括分隔符的数据。

去掉任何尾随的换行符:"\n""\r\n". 为了快速修复,请写:

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

例如,

package main

import (
    "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 world
ellohay orldway

理论要掌握,实操不能落!以上关于《字符串切片上的 For 循环迭代不起作用》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:Golang技术栈 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>