algorithm – How remove white spaces in Go-ThrowExceptions

Exception or error:

I want remove spaces in String value, for example: “I d skd a efju N” to “IdskdaefjuN” and it’s not working…

I tried using:

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)
    }
}
How to solve:

When I tried running your code it wasn’t reading from stdin properly using fmt.Scan(). If you replace it with a buffered reader that’s reading from stdin it works:

package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

func main() {
    //var String
    var stringValue string
    var originalValue string
    reader := bufio.NewReader(os.Stdin)
    fmt.Printf("please, type some text:")
    //user input
    stringValue, _ = reader.ReadString('\n')
    stringValue = strings.TrimSuffix(stringValue, "\n")
    originalValue = stringValue
    //remove spaces
    stringValue = strings.ReplaceAll(stringValue, " ", "")
    //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)
    }
}

Answer´╝Ü

How remove white spaces in Go


For example,

package main

import (
    "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)
}

Playground: https://play.golang.org/p/KoXZ24WXw7r

Output:

I d skd a efju N
IdskdaefjuN

Leave a Reply

Your email address will not be published. Required fields are marked *