遍历Go字符串以提取特定子字符串

huangapple go评论106阅读模式
英文:

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 (
	&quot;strings&quot;

) 
        
func find(input string){
	var result []string
	a := strings.SplitN(input, &quot;-(&quot;, 2);
	result[0] = a[0]
	b := strings.SplitN(a[1], &quot;) *(&quot;, 2)
	result[1] = b[0]
	c := strings.SplitN(a[1], &quot;) +(&quot;, 2)
	result[2] = c[0]
	d := strings.SplitN(a[1], &quot;)&quot;, 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.Fieldsstrings.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 &quot;fmt&quot;
import &quot;strings&quot;

func find(input string) []string {
	return strings.FieldsFunc(input, split)
}

func main() {
	strs := find(&quot;p -(40) *(GOT) +(FF)&quot;)
	str := strings.Replace(fmt.Sprint(strs), &quot; &quot;, &quot;, &quot;, -1)
	fmt.Println(str)
}

func split(r rune) bool {
	return r == &#39; &#39; || r == &#39;-&#39; || r == &#39;(&#39; || r == &#39;)&#39; || r == &#39;*&#39; || r == &#39;+&#39;
}

output:


You may use strings.Fields then strings.Trim, like this working sample code:

<!-- language: lang-golang -->

package main

import &quot;fmt&quot;
import &quot;strings&quot;

func main() {
	fmt.Println(find(&quot;p -(40) *(GOT) +(FF)&quot;))
}

func find(input string) []string {
	strs := strings.Fields(input)
	result := make([]string, 0, len(strs))

	for _, v := range strs {
		s := strings.Trim(v, &quot;()+-*&quot;)
		if len(s) &gt; 0 {
			result = append(result, s)
		}
	}
	return result
}

output:


Also this works for your special case:

<!-- language: lang-golang -->

package main

import &quot;fmt&quot;
import &quot;strings&quot;

func main() {
	fmt.Println(find(&quot;p -(40) *(GOT) +(FF)&quot;))
}

func find(input string) []string {
	strs := strings.Fields(input)
	for i := 0; i &lt; len(strs); i++ {
		strs[i] = strings.Trim(strs[i], &quot;()+-*&quot;)
	}
	return strs
}

output:


And if you need output like this string:

, try this working sample code:

<!-- language: lang-golang -->

package main

import &quot;fmt&quot;
import &quot;strings&quot;

func main() {
	strs := find(&quot;p -(40) *(GOT) +(FF)&quot;)
	str := strings.Replace(fmt.Sprint(strs), &quot; &quot;, &quot;, &quot;, -1)
	fmt.Println(str)
}

func find(input string) []string {
	strs := strings.Fields(input)
	for i := 0; i &lt; len(strs); i++ {
		strs[i] = strings.Trim(strs[i], &quot;()+-*&quot;)
	}
	return strs
}

output:


huangapple
  • 本文由 发表于 2016年8月22日 23:02:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/39082892.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定