go - 对于字符串 slice 的循环迭代不起作用

标签 go slice

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

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”作为我的短语,也会发生同样的事情。

最佳答案

Package bufio

func (*Reader) ReadString

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

ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter. If ReadString encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often io.EOF). ReadString returns err != nil if and only if the returned data does not end in delim.


returning a string containing the data up to and including the delimiter.

去掉任何结尾的换行符:"\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

关于go - 对于字符串 slice 的循环迭代不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52712160/

相关文章:

struct - 如何在 slice 内设置结构的字段?

go - 从 PEM 解析和打印 X.509 属性

带有映射诊断上下文的 Golang 日志记录

debugging - Delve (dlv) 无法附加到进程

go - 不能在 strconv.ParseFloat 问题的参数中使用 (type []byte) 作为类型字符串

Javascript:根据属性值将对象数组拆分为具有动态名称的单独数组

python - 通过切片和 for 循环修改 Python 列表?

python - 根据索引列表从numpy数组中获取和替换值

arrays - 使用切片和使用对数组的引用作为参数有什么区别?

go - "unexpected end of JSON input"当在 POST 请求中传递对象数组时