How to extract numbers from string in Go language
How to extract numbers from a string in Go language
In Go language, we often need to extract the number part from a string. For example, we might need to extract the numeric portion of a string that contains a phone number, or extract the price number from a string that contains price information. This article will introduce several common methods to achieve this goal.
Method 1: Use regular expressions
Go language provides a built-in regular expression libraryregexp
, which we can use to extract the numeric part of the string. The following is a sample code:
package main import ( "fmt" "regexp" ) func main() { str := "ABC1234DEF5678GHI" re := regexp.MustCompile("[0-9]+") nums := re.FindAllString(str, -1) fmt.Println(nums) }
Run the above code, the output result is: [1234 5678]
.
In the above code, we use the regular expression [0-9]
to match the numeric part of the string. Function FindAllString
Returns all matching strings. The second parameter -1
means to return all matches instead of just the first match. Finally, we print out the extracted number part.
Method 2: Use the strconv
package
The strconv
package of Go language provides a series of functions for converting strings and various numeric types. function. We can use these functions to extract numbers from a string.
The following is a sample code:
package main import ( "fmt" "strconv" ) func main() { str := "ABC1234DEF5678GHI" nums := make([]int, 0) num := "" for _, char := range str { if char >= '0' && char <= '9' { num += string(char) } else if num != "" { n, _ := strconv.Atoi(num) nums = append(nums, n) num = "" } } fmt.Println(nums) }
Run the above code, the output result is: [1234 5678]
.
In the above code, we iterate through each character in the string and convert the extracted numeric string to an integer using the Atoi
function. Finally, we print out the extracted number part.
Method 3: Manually parse the string
If the number part in the string has certain rules, we can also manually parse the string to extract the number. The following is a sample code:
package main import "fmt" func main() { str := "ABC1234DEF5678GHI" nums := make([]int, 0) num := 0 for _, char := range str { if char >= '0' && char <= '9' { num = num*10 + int(char-'0') } else if num != 0 { nums = append(nums, num) num = 0 } } fmt.Println(nums) }
Run the above code, the output result is: [1234 5678]
.
In the above code, we iterate through each character in the string and use the ASCII code of the current character minus the ASCII code of the character '0'
to get the corresponding number . By constantly multiplying the current number by 10 and adding the new number, we get the final number. Finally, we print out the extracted number part.
Through the above introduction, we can see that the Go language provides a variety of methods to extract the numeric part of the string. We can choose the appropriate method based on specific needs. No matter which method we choose, we can accurately extract the numeric part of the string, which facilitates our subsequent processing.
The above is the detailed content of How to extract numbers from string in Go language. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...
