go - strings - get characters before a digit -


i have strings such e2 9nz, n29dz, ew29dz . need extract chars before first digit, given above example : e, n, ew. supposed use regex ? strings package looks nice doesn't seem handle case (extract before specific type).

edit:

to clarify "question" i'm wondering method more idiomatic go , perhaps provide better performance.

for example,

package main  import (     "fmt"     "unicode" )  func digitprefix(s string) string {     i, r := range s {         if unicode.isdigit(r) {             return s[:i]         }     }     return s }  func main() {     fmt.println(digitprefix("e2 9nz"))     fmt.println(digitprefix("n29dz"))     fmt.println(digitprefix("ew29dz"))     fmt.println(digitprefix("wxyz")) } 

output:

e n ew wxyz 

if there no digit, example "wxyz", , don't want returned, change return s return "".


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -