Remove digits from string

6,003 views
Skip to first unread message

Vincent Ambo

unread,
Jan 13, 2011, 6:52:04 AM1/13/11
to golang-nuts
Hey,

if I have a string, lets say
var test string = "The number is 12345"

How can I trim everything from the string except for the digits?

Thanks in advance!

chris dollin

unread,
Jan 13, 2011, 7:02:16 AM1/13/11
to Vincent Ambo, golang-nuts
On 13 January 2011 11:52, Vincent Ambo <taz...@googlemail.com> wrote:
> Hey,
>
> if I have a string, lets say
> var test string = "The number is 12345"
>
> How can I trim everything from the string except for the digits?

My first (not necessarily best) thought would be to use
regular expressions:

package main

import "regexp"
import "fmt"

func main() {
re := regexp.MustCompile( "[^0-9]" )
fmt.Printf( re.ReplaceAllString( "hello 12 34 five", "" ) )
}

Chris

[Typed into goplay for testing ...]

--
Chris "allusive" Dollin

peterGo

unread,
Jan 13, 2011, 7:17:37 AM1/13/11
to golang-nuts
Vincent,

You can write a Trim function to do that. For example,

package main

import (
"fmt"
"strings"
)

func TrimToNum(r int) bool {
if n := r - '0'; n >= 0 && n <= 9 {
return false
}
return true
}

func main() {
s := "The number is 12345"
n := strings.TrimFunc(s, TrimToNum)
fmt.Println(n)
}

Peter

peterGo

unread,
Jan 13, 2011, 7:25:29 AM1/13/11
to golang-nuts
Chris,

Your code doesn't "trim everything from the string except for the
digits." It replaces, it doesn't trim.

Peter

chris dollin

unread,
Jan 13, 2011, 7:39:28 AM1/13/11
to peterGo, golang-nuts
On 13 January 2011 12:17, peterGo <go.pe...@gmail.com> wrote:

> func TrimToNum(r int) bool {
>        if n := r - '0'; n >= 0 && n <= 9 {
>                return false
>        }
>        return true
> }

Why not

return r < '0' || '9' < r

?

Chris

--
Chris "allusive" Dollin

chris dollin

unread,
Jan 13, 2011, 7:41:31 AM1/13/11
to peterGo, golang-nuts
On 13 January 2011 12:25, peterGo <go.pe...@gmail.com> wrote:

> Your code doesn't "trim everything from the string except for the
> digits." It replaces, it doesn't  trim.

It's not clear (to me) whether the OP was limiting themselves
to "trim from the ends". That's why the example I gave explicitly
had a space embedded in the digit sequence, so that if that's
not what they wanted they could have said so.

Russ Cox

unread,
Jan 14, 2011, 3:01:08 PM1/14/11
to Vincent Ambo, golang-nuts
There is also strings.Map.

Mitul sharma

unread,
Dec 29, 2022, 12:50:34 PM12/29/22
to golang-nuts
Hello,

I'm stuck in below code snippet, looking to have a output as string without any digits.
Input : "H1e5ll0o"
Output: "Hello"

I tried with output := strings.Replace(str, "1", "", -1), but it can replace only single digit at a time

I'm looking for suggestions for below code snippet , tried to implement a logic where string converted to rune and rune provided
as a input to IsDigit(), if it is digit it should replace the digit with "" and print the string , i'm not sure what mistake am i doing in providing parameter
and printing output?

var output string

var r rune
for _, r = range input {
break
}

if unicode.IsDigit(rune(r)) {
output = strings.Replace(input, "", "", -1)
fmt.Println(output)
}else{
fmt.Println(input)
}



On Saturday, 15 January 2011 at 01:31:08 UTC+5:30 Russ Cox wrote:
There is also strings.Map.

peterGo

unread,
Dec 29, 2022, 5:12:47 PM12/29/22
to golang-nuts

Brian Candler

unread,
Dec 30, 2022, 6:35:10 AM12/30/22
to golang-nuts
<bikeshed>

peterGo

unread,
Dec 30, 2022, 8:55:31 AM12/30/22
to golang-nuts
Brian,  

The Bikeshed email      
http://phk.freebsd.dk/sagas/bikeshed/      

Regular Expressions: Now You Have Two Problems      
https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/      

MustCompile is like Compile but panics if the expression cannot be parsed. It simplifies safe initialization of global variables holding compiled regular expressions.    
https://pkg.go.dev/reg...@go1.19.4#MustCompile    

You use MustCompile for a local variable.    
https://go.dev/play/p/WxKPohZhbrE    

BenchmarkBrianLocal-4     501051  2407 ns/op    797 B/op  12 allocs/op

Using MustCompile for a global variable.    

BenchmarkBrianGlobal-4   1243546   965.9 ns/op   32 B/op   3 allocs/op

I used strings.Map.    
https://go.dev/play/p/F6gNFWjRzGO    

BenchmarkPeter-4        10065352   111.8 ns/op   16 B/op   1 allocs/op

peter
Reply all
Reply to author
Forward
0 new messages