-2

I have string with arabic number. How to replace this character from map numbers?

String: 000۲۱۷۹۰۰

my code:

var digitconv = map[string]int{"۰":0, "۱":1, "۲":2, "۳":3, "۴":4, "۵":5, "۶":6, "۷":7, "۸":8, "۹":9}

func main() {
    num := "000۲۱۷۹۰۰"
    chk, _ := regexp.Match("", num)
    fmt.Println(chk)
}
1
  • 2
    Don't use a regex. Regex is almost always the wrong tool. Just use strings.ReplaceAll in a for loop, or strings.Map or a strings.Replacer. Jan 19, 2021 at 13:32

1 Answer 1

3

Simply use strings.Map():

var digitconv = map[rune]rune{
    '۰': '0', '۱': '1', '۲': '2', '۳': '3', '۴': '4',
    '۵': '5', '۶': '6', '۷': '7', '۸': '8', '۹': '9',
}
num := "000۲۱۷۹۰۰"
fmt.Println(num)

s := strings.Map(func(r rune) rune {
    if r2, ok := digitconv[r]; ok {
        return r2
    }
    return r
}, num)

fmt.Println(s)

Output (try it on the Go Playground):

000۲۱۷۹۰۰
000217900

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy