如何删除字符串模式及其后面的所有字符串?

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

How to remove string pattern and all the string behind that pattern?

问题

例如:

package main

import "fmt"

func main() {
    pattern := "helloworld."
    myString := "foo.bar.helloworld.qwerty.zxc.helloworld.asd"
    fmt.Println(removeFromPattern(pattern, myString))
}

func removeFromPattern(p, ms string) string {
    // 我在这里感到困惑(以高效的方式)
}

期望的输出:

qwerty.zxc.helloworld.asd

我如何获得期望的输出,还有如何从myString中删除第一个pattern及其后面的所有字符串?

英文:

For Example :

package main

import "fmt"

func main() {
    pattern := "helloworld."
    myString := "foo.bar.helloworld.qwerty.zxc.helloworld.asd"
    fmt.Println(removeFromPattern(pattern, myString))
}

func removeFromPattern(p, ms string) string {
    // I confused here (in efficient way)
}

Wanted output :

qwerty.zxc.helloworld.asd

How do I get that wanted output, also how to remove the first pattern and all the strings behind that pattern from myString ?

答案1

得分: 1

1- 使用_, after, _ = strings.Cut(ms, p),尝试这个

func removeFromPattern(p, ms string) (after string) {
	_, after, _ = strings.Cut(ms, p) // 分隔符前后的文本
	return
}

其中使用了strings.Index

// Cut函数在s中找到第一个sep的实例,并将s分割成sep之前和之后的文本。
// found表示sep是否出现在s中。
// 如果sep不在s中,cut返回s、""和false。
func Cut(s, sep string) (before, after string, found bool) {
	if i := Index(s, sep); i >= 0 {
		return s[:i], s[i+len(sep):], true
	}
	return s, "", false
}

2- 使用strings.Index,尝试这个

func removeFromPattern(p, ms string) string {
	i := strings.Index(ms, p)
	if i == -1 {
		return ""
	}
	return ms[i+len(p):]
}

3- 使用strings.Split,尝试这个

func removeFromPattern(p, ms string) string {
	a := strings.Split(ms, p)
	if len(a) != 2 {
		return ""
	}
	return a[1]
}

4- 使用regexp,尝试这个

func removeFromPattern(p, ms string) string {
	a := regexp.MustCompile(p).FindStringSubmatch(ms)
	if len(a) < 2 {
		return ""
	}
	return a[1]
}
英文:

1- Using _, after, _ = strings.Cut(ms, p), try this:

func removeFromPattern(p, ms string) (after string) {
	_, after, _ = strings.Cut(ms, p) // before and after sep.
	return
}

Which uses strings.Index :

// Cut slices s around the first instance of sep,
// returning the text before and after sep.
// The found result reports whether sep appears in s.
// If sep does not appear in s, cut returns s, &quot;&quot;, false.
func Cut(s, sep string) (before, after string, found bool) {
	if i := Index(s, sep); i &gt;= 0 {
		return s[:i], s[i+len(sep):], true
	}
	return s, &quot;&quot;, false
}

2- Using strings.Index, try this:

func removeFromPattern(p, ms string) string {
	i := strings.Index(ms, p)
	if i == -1 {
		return &quot;&quot;
	}
	return ms[i+len(p):]
}

3- Using strings.Split, try this:

func removeFromPattern(p, ms string) string {
	a := strings.Split(ms, p)
	if len(a) != 2 {
		return &quot;&quot;
	}
	return a[1]
}

4- Using regexp, try this

func removeFromPattern(p, ms string) string {
	a := regexp.MustCompile(p).FindStringSubmatch(ms)
	if len(a) &lt; 2 {
		return &quot;&quot;
	}
	return a[1]
}

答案2

得分: 1

func main() {
	pattern := "helloworld."
	myString := "foo.bar.helloworld.qwerty.zxc"

	res := removeFromPattern(pattern, myString)
	fmt.Println(res)
}

func removeFromPattern(p, ms string) string {
	parts := strings.Split(ms, p)
	if len(parts) > 1 {
		return parts[1]
	}
	return ""
}

<details>
<summary>英文:</summary>

[strings.Split][1] is enough

```go
func main() {
	pattern := &quot;helloworld.&quot;
	myString := &quot;foo.bar.helloworld.qwerty.zxc&quot;

	res := removeFromPattern(pattern, myString)
	fmt.Println(res)
}

func removeFromPattern(p, ms string) string {
	parts := strings.Split(ms, p)
	if len(parts) &gt; 1 {
		return parts[1]
	}
	return &quot;&quot;
}

答案3

得分: 0

func removeFromPattern(p, ms string) string {
	return strings.ReplaceAll(ms, p, "")
}
func main() {
	pattern := "helloworld."
	myString := "foo.bar.helloworld.qwerty.zxc"
	res := removeFromPattern(pattern, myString)
	fmt.Println(res)
}
func removeFromPattern(p, ms string) string {
	return strings.ReplaceAll(ms, p, "")
}
func main() {
	pattern := "helloworld."
	myString := "foo.bar.helloworld.qwerty.zxc"
	res := removeFromPattern(pattern, myString)
	fmt.Println(res)
}
英文:
func removeFromPattern(p, ms string) string {
	return strings.ReplaceAll(ms, p, &quot;&quot;)
}
func main() {
	pattern := &quot;helloworld.&quot;
	myString := &quot;foo.bar.helloworld.qwerty.zxc&quot;
	res := removeFromPattern(pattern, myString)
	fmt.Println(res)
}

huangapple
  • 本文由 发表于 2022年5月24日 19:14:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/72361885.html
匿名

发表评论

匿名网友

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

确定