Home >Backend Development >Golang >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!