Search code examples
godouble-byte

Identify double byte character in a string and convert that into a single byte character


In my Go project, I am dealing with asian languages and There are double byte characters. In my case, I have a string which contains two words and there is a space between them.

EG: こんにちは 世界

Now I need to check if that space is a double byte space and if so, I need to convert that into single byte space.

I have searched a lot, but I couldn't find a way to do this. Since I cannot figure out a way to do this, sorry I have no code sample to add here.

Do I need to loop through each character and pick the double byte space using its code and replace? What is the code I should use to identify double byte space?


Solution

  • Just replace?

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main()  {
        fmt.Println(strings.Replace("こんにちは 世界", " ", " ", -1))
    }
    

    Notice that the second argument in Replace is  , as copy-paste from your string in example. This replace function will find all rune that match that in the original string and replace it with ASCII space