英文:
How to replace multiple slashes (///) in a string to one slash (/) in Go lang?
问题
"/hello/stackover.flow"
英文:
Input string: "///hello//stackover.flow"
Expected output: "/hello/stackover.flow"
答案1
得分: 7
你可以使用path.Clean
来实现这个功能。
func Clean(path string) string
Clean函数通过纯粹的词法处理返回与路径path等效的最短路径名。它按照以下规则迭代应用,直到无法进行进一步处理为止:
- 将多个斜杠替换为单个斜杠。
- 消除每个“.”路径名元素(当前目录)。
- 消除每个内部的“..”路径名元素(父目录)以及它之前的非“..”元素。
- 消除以根路径开始的“..”元素:也就是将路径开头的“/..”替换为“/”。
返回的路径只有在根目录“/”时才以斜杠结尾。
如果此过程的结果是空字符串,则Clean函数返回字符串“.”。
下面是一个简单的基准测试,将其与正则表达式解决方案进行比较:
package main
import (
"path"
"regexp"
"testing"
)
var p = "///hello//stackover.flow"
func BenchmarkPathRegexp(b *testing.B) {
re := regexp.MustCompile("/+")
for i := 0; i < b.N; i++ {
re.ReplaceAllLiteralString(p, "/")
}
}
func BenchmarkPathClean(b *testing.B) {
for i := 0; i < b.N; i++ {
path.Clean(p)
}
}
结果:
BenchmarkPathRegexp-4 2000000 794 ns/op
BenchmarkPathClean-4 10000000 145 ns/op
英文:
You can use path.Clean
for that.
> func Clean(path string) string
> Clean returns the shortest path name equivalent to path by purely lexical processing. It applies the following rules iteratively until no further processing can be done:
> 1. Replace multiple slashes with a single slash.
> 2. Eliminate each . path name element (the current directory).
> 3. Eliminate each inner .. path name element (the parent directory)
along with the non-.. element that precedes it.
> 4. Eliminate .. elements that begin a rooted path:
that is, replace "/.." by "/" at the beginning of a path.
The returned path ends in a slash only if it is the root "/".
> If the result of this process is an empty string, Clean returns the string ".".
And here's a simple benchmark which compares it with regexp solution:
package main
import (
"path"
"regexp"
"testing"
)
var p = "///hello//stackover.flow"
func BenchmarkPathRegexp(b *testing.B) {
re := regexp.MustCompile("/+")
for i := 0; i < b.N; i++ {
re.ReplaceAllLiteralString(p, "/")
}
}
func BenchmarkPathClean(b *testing.B) {
for i := 0; i < b.N; i++ {
path.Clean(p)
}
}
Results:
BenchmarkPathRegexp-4 2000000 794 ns/op
BenchmarkPathClean-4 10000000 145 ns/op
答案2
得分: 1
只是一个选项。如果你需要替换其他多个字符,你可以使用它。
re, _ := regexp.Compile("/+")
fmt.Println(re.ReplaceAllLiteralString("///hello//stackover.flow", "/"))
英文:
Just an option. You can use it if you need to replace some other multiple characters.
re, _ := regexp.Compile("/+")
fmt.Println(re.ReplaceAllLiteralString("///hello//stackover.flow", "/"))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论