Replace numbers by zero from string
In this example we replace any number of 1 or 2 digit by 0.
Example
package main
import "fmt"
func main() {
str := "abc123465@ahsjf222"
newStr := make([]rune, len(str))
i, added := 0, false
for _, r := range str {
if r >= '0' && r <= '9' {
if added {
continue
}
added, newStr[i] = true, '0'
} else {
added, newStr[i] = false, r
}
i++
}
fmt.Println(string(newStr[:i]))
}
Output
abc0@ahsjf0
Most Helpful This Week
How to find length of Map in Go?
How to check string contains uppercase lowercase character in Golang?
Regular expression to validate common Credit Card Numbers
How to find out element position in slice?
How to set timeout for http.Get() requests in Golang?
How to get the current date and time with timestamp in local and other timezones ?
How to Draw a rectangle in Golang?
How to append text to a file in Golang?
How to check if a map contains a key in Go?
Simple function with parameters in Golang