对于字符串切片的循环循环不起作用 [英] For Loop iteration over string slice doesn't work
问题描述
我写了这段代码,应该将一个小写的英语短语翻译成猪拉丁语.
I wrote this code, which should translate a lower case english phrase into 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, " "))
}
但是,它对词组中的最后一个词没有任何作用.
However, it fails to do anything to the last word in the phrase.
如果我使用敏捷的棕色狐狸跳过懒狗"的句子 我收到"ethay uickqay ownbray oxfay umpedjay overay ethay azylay狗"
If I use the sentence "the quick brown fox jumped over the lazy dog" I get "ethay uickqay ownbray oxfay umpedjay overay ethay azylay dog"
这里的所有内容都对,除了最后一个字!为什么不起作用? 如果我使用"hello world"作为短语,也会发生同样的事情.
Everything here is right, except the last word! why doesn't it work? The same thing happens if I use "hello world" as my phrase.
推荐答案
func (b *Reader) ReadString(delim byte) (string, error)
ReadString读取直到输入中第一次出现delim为止, 返回一个字符串,该字符串包含直至(包括)该数据的数据 定界符.如果ReadString在找到 定界符,它返回错误之前读取的数据和错误 本身(通常是io.EOF). ReadString返回err!= nil仅当且仅当 返回的数据不以delim结尾.
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"
.要快速修复,请输入:
Get rid of any trailing newlines: "\n"
or "\r\n"
. For a quick fix, write:
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
这篇关于对于字符串切片的循环循环不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!