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

时间:2018-10-01 01:14:04

标签: go slice

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

BEGIN
  UPDATE tbl_stock
     SET quantity = quantity + NEW.quantity
   WHERE vegetableCultivar = NEW.vegetableCultivar
   AND warehouse = NEW.warehouse;
     if sql%rowcount = 0 then  
    -- nothing was updated, so the record doesn't exist, insert it. 
    insert into tbl_stock (warehouse, vegetableCultivar, vegetableVariety, quantity)
            values (NEW.warehouse, NEW.vegetableCultivar, NEW.vegetableVariety, NEW.quantity);
  end if;
END

但是,它对短语中的最后一个单词没有任何作用。

如果我使用“敏捷的棕色狐狸跳过懒狗”的句子 我收到“ ethay uickqay ownbray oxfay umpedjay overay ethay azylay狗”

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

1 个答案:

答案 0 :(得分:0)

  

Package bufio

     

func (*Reader) ReadString

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

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


  

返回一个字符串,该字符串包含直至(包括)   定界符。

摆脱任何结尾的换行符:"\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
相关问题