英文:
Iterating over Go string to extract specific substrings
问题
我开始学习Go,并且想要实现一些算法。我想要遍历一个字符串,然后提取一些子字符串,例如:p -(40) *(GOT) +(FF)
应该返回一个类似这样的数组:。
我写了一些代码,但是有点模糊:
import (
"strings"
)
func find(input string){
var result []string
a := strings.SplitN(input, "-(", 2)
result[0] = a[0]
b := strings.SplitN(a[1], ") *(", 2)
result[1] = b[0]
c := strings.SplitN(a[1], ") +(", 2)
result[2] = c[0]
d := strings.SplitN(a[1], ")", 2)
result[3] = d[0]
}
有人可以纠正我或者提供更简单的方法吗?谢谢!
英文:
I started learning Go and I want to implement some algorithm.
I want to iterate over a string and then extract some substrings for example : p -(40) *(GOT) +(FF)
should return an array like this:
I wrote something like this but it's vague:
<!-- language: lang-golang -->
import (
"strings"
)
func find(input string){
var result []string
a := strings.SplitN(input, "-(", 2);
result[0] = a[0]
b := strings.SplitN(a[1], ") *(", 2)
result[1] = b[0]
c := strings.SplitN(a[1], ") +(", 2)
result[2] = c[0]
d := strings.SplitN(a[1], ")", 2)
result[3] = d[0]
}
Can someone please correct me or suggest something easier? Thanks!
答案1
得分: 1
你可以使用strings.FieldsFunc
函数,像下面这个示例代码一样:
package main
import "fmt"
import "strings"
func find(input string) []string {
return strings.FieldsFunc(input, split)
}
func main() {
strs := find("p -(40) *(GOT) +(FF)")
str := strings.Replace(fmt.Sprint(strs), " ", ", ", -1)
fmt.Println(str)
}
func split(r rune) bool {
return r == ' ' || r == '-' || r == '(' || r == ')' || r == '*' || r == '+'
}
输出结果为:
你也可以使用strings.Fields
和strings.Trim
函数,像下面这个示例代码一样:
package main
import "fmt"
import "strings"
func main() {
fmt.Println(find("p -(40) *(GOT) +(FF)"))
}
func find(input string) []string {
strs := strings.Fields(input)
result := make([]string, 0, len(strs))
for _, v := range strs {
s := strings.Trim(v, "()+-*")
if len(s) > 0 {
result = append(result, s)
}
}
return result
}
输出结果为:
对于你的特殊情况,也可以使用下面这个示例代码:
package main
import "fmt"
import "strings"
func main() {
fmt.Println(find("p -(40) *(GOT) +(FF)"))
}
func find(input string) []string {
strs := strings.Fields(input)
for i := 0; i < len(strs); i++ {
strs[i] = strings.Trim(strs[i], "()+-*")
}
return strs
}
输出结果为:
如果你需要输出形如的字符串,可以尝试下面这个示例代码:
package main
import "fmt"
import "strings"
func main() {
strs := find("p -(40) *(GOT) +(FF)")
str := strings.Replace(fmt.Sprint(strs), " ", ", ", -1)
fmt.Println(str)
}
func find(input string) []string {
strs := strings.Fields(input)
for i := 0; i < len(strs); i++ {
strs[i] = strings.Trim(strs[i], "()+-*")
}
return strs
}
输出结果为:
英文:
You may use strings.FieldsFunc
, like this working sample code:
<!-- language: lang-golang -->
package main
import "fmt"
import "strings"
func find(input string) []string {
return strings.FieldsFunc(input, split)
}
func main() {
strs := find("p -(40) *(GOT) +(FF)")
str := strings.Replace(fmt.Sprint(strs), " ", ", ", -1)
fmt.Println(str)
}
func split(r rune) bool {
return r == ' ' || r == '-' || r == '(' || r == ')' || r == '*' || r == '+'
}
output:
You may use strings.Fields
then strings.Trim
, like this working sample code:
<!-- language: lang-golang -->
package main
import "fmt"
import "strings"
func main() {
fmt.Println(find("p -(40) *(GOT) +(FF)"))
}
func find(input string) []string {
strs := strings.Fields(input)
result := make([]string, 0, len(strs))
for _, v := range strs {
s := strings.Trim(v, "()+-*")
if len(s) > 0 {
result = append(result, s)
}
}
return result
}
output:
Also this works for your special case:
<!-- language: lang-golang -->
package main
import "fmt"
import "strings"
func main() {
fmt.Println(find("p -(40) *(GOT) +(FF)"))
}
func find(input string) []string {
strs := strings.Fields(input)
for i := 0; i < len(strs); i++ {
strs[i] = strings.Trim(strs[i], "()+-*")
}
return strs
}
output:
And if you need output like this string: , try this working sample code:
<!-- language: lang-golang -->
package main
import "fmt"
import "strings"
func main() {
strs := find("p -(40) *(GOT) +(FF)")
str := strings.Replace(fmt.Sprint(strs), " ", ", ", -1)
fmt.Println(str)
}
func find(input string) []string {
strs := strings.Fields(input)
for i := 0; i < len(strs); i++ {
strs[i] = strings.Trim(strs[i], "()+-*")
}
return strs
}
output:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论