func(* Reader)ReadString
func (b *Reader) ReadString(delim byte) (string, error)ReadString读取直到输入中第一次出现delim为止,并返回一个字符串,其中包含直到定界符(包括定界符)的数据。
name终端输入包括尾随换行符。例如,
package mainimport ( "bufio" "fmt" "os")func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Enter name:") name, err := reader.ReadString('n') if err != nil { panic(err) } fmt.Printf("%qn", name) fmt.Println(name == "nixon")}
输出(Linux):
Enter name:nixon"nixonn"false
输出(Windows):
Enter name:nixon"nixonrn"false
修剪尾随换行符。例如,
package mainimport ( "bufio" "fmt" "os" "strings")func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Enter name:") name, err := reader.ReadString('n') if err != nil { panic(err) } fmt.Printf("%qn", name) name = strings.TrimRight(name, "rn") fmt.Printf("%qn", name) fmt.Println(name == "nixon")
}
输出(Linux):
Enter name:nixon"nixonn""nixon"true
输出(Windows):
Enter name:nixon"nixonrn""nixon"true
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)