猿问

Go 中如何删除空格

我想删除字符串值中的空格,例如:“I d skd a efju N”到“IdskdaefjuN”,但它不起作用......


我尝试使用:


stringValue = strings.Replace(stringValue, " ", "", -1)

package main


import (

    "fmt"

    "strings"

)


func main() {

    //var String

    var stringValue string

    var originalValue string

    fmt.Printf("please, type some text:")

    //user input

    fmt.Scan(&stringValue)

    originalValue = stringValue

    //remove spaces

    stringValue = strings.Replace(stringValue, " ", "", -1)

    //set string to lower case

    stringValue = strings.ToLower(stringValue)

    //if contains substring "i", "a" and "n"

    if strings.Contains(stringValue, "i") && strings.Contains(stringValue, "a") && strings.Contains(stringValue, "n") {

        // if "i" is a prefix and "n" a suffix

        firstChar := strings.HasPrefix(stringValue, "i")

        lastChar := strings.HasSuffix(stringValue, "n")

        switch {

        //if is a prefix and suffix so Found

        case firstChar && lastChar:

            fmt.Printf("Found in %s", originalValue)

        //if isnt a prefix and suffix so Not Found

        default:

            fmt.Printf("Not Found in %s", originalValue)

        }

        //if there's no "i", "a" and "n"

    } else {

        fmt.Printf("Not Found in %s", originalValue)

    }

}


繁华开满天机
浏览 120回答 4
4回答

30秒到达战场

Go 中如何删除空格例如,package mainimport (    "fmt"    "unicode")func removeSpace(s string) string {    rr := make([]rune, 0, len(s))    for _, r := range s {        if !unicode.IsSpace(r) {            rr = append(rr, r)        }    }    return string(rr)}func main() {    s := "I d skd a efju N"    fmt.Println(s)    s = removeSpace(s)    fmt.Println(s)}游乐场:https://play.golang.org/p/KoXZ24WXw7r输出:I d skd a efju NIdskdaefjuN

喵喵时光机

我用过这个func cleanSpaces(stringToClean string) string {&nbsp; &nbsp; result := strings.ReplaceAll(stringToClean, " ", "")&nbsp; &nbsp; return result}或者func cleanSpaces(stringToClean string) string {&nbsp; &nbsp; fields := strings.Fields(stringToClean)&nbsp; &nbsp; var result string&nbsp; &nbsp; for i := 0; i < len(fields); i++ {&nbsp; &nbsp; &nbsp; &nbsp;result += fields[i]&nbsp; &nbsp; }&nbsp; &nbsp; return result}

手掌心

当我尝试运行您的代码时,它没有正确使用fmt.Scan(). 如果将其替换为从 stdin 读取的缓冲读取器,则它可以工作:package mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "os"&nbsp; &nbsp; "strings")func main() {&nbsp; &nbsp; //var String&nbsp; &nbsp; var stringValue string&nbsp; &nbsp; var originalValue string&nbsp; &nbsp; reader := bufio.NewReader(os.Stdin)&nbsp; &nbsp; fmt.Printf("please, type some text:")&nbsp; &nbsp; //user input&nbsp; &nbsp; stringValue, _ = reader.ReadString('\n')&nbsp; &nbsp; stringValue = strings.TrimSuffix(stringValue, "\n")&nbsp; &nbsp; originalValue = stringValue&nbsp; &nbsp; //remove spaces&nbsp; &nbsp; stringValue = strings.ReplaceAll(stringValue, " ", "")&nbsp; &nbsp; //set string to lower case&nbsp; &nbsp; stringValue = strings.ToLower(stringValue)&nbsp; &nbsp; //if contains substring "i", "a" and "n"&nbsp; &nbsp; if strings.Contains(stringValue, "i") && strings.Contains(stringValue, "a") && strings.Contains(stringValue, "n") {&nbsp; &nbsp; &nbsp; &nbsp; // if "i" is a prefix and "n" a suffix&nbsp; &nbsp; &nbsp; &nbsp; firstChar := strings.HasPrefix(stringValue, "i")&nbsp; &nbsp; &nbsp; &nbsp; lastChar := strings.HasSuffix(stringValue, "n")&nbsp; &nbsp; &nbsp; &nbsp; switch {&nbsp; &nbsp; &nbsp; &nbsp; //if is a prefix and suffix so Found&nbsp; &nbsp; &nbsp; &nbsp; case firstChar && lastChar:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Found in %s", originalValue)&nbsp; &nbsp; &nbsp; &nbsp; //if isnt a prefix and suffix so Not Found&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Not Found in %s", originalValue)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //if there's no "i", "a" and "n"&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Not Found in %s", originalValue)&nbsp; &nbsp; }}

jeck猫

这对我有用package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strings")func main() {&nbsp; &nbsp; x := "&nbsp; &nbsp; &nbsp; &nbsp;1&nbsp; &nbsp; ,2,&nbsp; &nbsp;3,4,6&nbsp; &nbsp; &nbsp;-3&nbsp; &nbsp;0, 7,&nbsp; &nbsp;8,70&nbsp; &nbsp;-9&nbsp; &nbsp;0"&nbsp; &nbsp; x = strings.Replace(x, " ", "", -1)&nbsp; &nbsp; fmt.Println(x)}只需将空格替换为空字符串,请参阅此处的官方文档
随时随地看视频慕课网APP

相关分类

Go
我要回答