Go (strings) – 去除重复的前缀/后缀子字符串

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

Go (strings) - Trim repeated prefix/suffix substring

问题

我需要修剪字符串中重复的前缀和后缀子字符串("%20")。

示例:%20%20Hello%20World%20%20 --> Hello%20World

我能想到的最好的方法是:

func trimPrefix(s string, prefix string) string {
    for strings.HasPrefix(s, prefix) {
        s = s[len(prefix):]
    }
    return s
}

func trimSuffix(s string, suffix string) string {
    for strings.HasSuffix(s, suffix) {
        s = s[:len(s)-len(suffix)]
    }
    return s
}

func trim(s, ss string) string {
    return trimPrefix(trimSuffix(s, ss), ss)
}

在Go语言中是否有更优雅的方法来实现这个功能呢?

英文:

I need to trim a repeated prefix and suffix substring ("%20") of a string.
Example: %20%20Hello%20World%20%20 --> Hello%20World

The best I could come with is something like:

func trimPrefix(s string, prefix string) string {
	for ; strings.HasPrefix(s, prefix); {
		s = s[len(prefix):]
	}
	return s
}

func trimSuffix(s string, suffix string) string {
	for ; strings.HasSuffix(s, suffix); {
		s = s[:len(s)-len(suffix)]
	}
	return s
}

func trim(s, ss string) string {
	return trimPrefix(trimSuffix(s, ss), ss)
}

Is there a more elegant way to do it in Go?

答案1

得分: 3

你可以执行以下操作:

func trimSubstr(s string, substr string) (t string) {
	for {
		t = strings.TrimPrefix(s, substr)
		t = strings.TrimSuffix(t, substr)
		if t == s { // 如果没有从 s 中删除任何内容,则退出
			break
		}
		s = t // 更新为最后的结果
	}
	return t
}

https://go.dev/play/p/eIk6A8K3Q_1

英文:

You can do the following:

func trimSubstr(s string, substr string) (t string) {
	for {
		t = strings.TrimPrefix(s, substr)
		t = strings.TrimSuffix(t, substr)
		if t == s { // exit if nothing was trimmed from s
			break
		}
		s = t // update to last result
	}
	return t
}

https://go.dev/play/p/eIk6A8K3Q_1

答案2

得分: 2

strings.Trim可以实现这个功能。

Trim函数返回一个从字符串s中删除了cutset参数中包含的所有前导和尾随Unicode码点的切片。

package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "%20%20Hello%20World%20%20"
	t := strings.Trim(s, "%20")

	fmt.Println(t) // 输出 Hello%20World
}

如果你的前缀和后缀符文总是以cutset参数中给定的相同顺序出现,那么这个方法就可以正常工作。

如果你的输入字符串可能以不同的顺序出现这些字符(有关详细信息,请参见对此答案的评论),那么你的解决方案是正确的。你可以使用标准库中的strings.TrimPrefixstrings.TrimSuffix代替自己编写的函数,并将它们组合在一个函数中:

func trim(s, sub string) string {
	for strings.HasPrefix(s, sub) {
		s = strings.TrimPrefix(s, sub)
	}
	for strings.HasSuffix(s, sub) {
		s = strings.TrimSuffix(s, sub)
	}
	return s
}
英文:

strings.Trim does that

> Trim returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.

package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "%20%20Hello%20World%20%20"
	t := strings.Trim(s, "%20")

	fmt.Println(t) // prints Hello%20World
}

This works just fine if your prefix and suffix runes always appear in the same order given in cutset param.

If your input string may present those characters in different orders (see comments to this answer for details), then your solution is good. You can use strings.TrimPrefix and strings.TrimSuffix from the standard lib instead of rolling your own, and combine it in one function:

func trim(s, sub string) string {
	for strings.HasPrefix(s, sub) {
		s = strings.TrimPrefix(s, sub)
	}
	for strings.HasSuffix(s, sub) {
		s = strings.TrimSuffix(s, sub)
	}
	return s
}

huangapple
  • 本文由 发表于 2022年2月16日 15:16:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/71137674.html
匿名

发表评论

匿名网友

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

确定