如何在Go中根据分隔符的第二次出现来拆分字符串?

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

How to split a string based on second occurence of delimiter in Go?

问题

我有一个字符串 a_b_c_d_e。我想将它分割成 a_bc_d_e。有什么好的方法可以做到这一点?

目前我知道如何根据第一个下划线来分割字符串,可以使用 SplitN 函数:

strings.SplitN(str, "_", 2)

如果 str 是 a_b_c_d_e,那么输出将是 ab_c_d_e

英文:

I have a string a_b_c_d_e. I want to split it as a_b and c_d_e. What would be a good a way to do it?

Currently I know how to split a string based on first underscore by using the SplitN function:

strings.SplitN(str, "_", 2)

If str is a_b_c_d_e then output will be a and b_c_d_e.

答案1

得分: 1

你想要的东西不存在,据我所知。所以你只需要自己创建:

package hello

func split(s string, sep rune, n int) (string, string) {
   for i, sep2 := range s {
      if sep2 == sep {
         n--
         if n == 0 {
            return s[:i], s[i+1:]
         }
      }
   }
   return s, ""
}
英文:

What you want doesn't exist, that I know of. So you just need to make your own:

package hello

func split(s string, sep rune, n int) (string, string) {
   for i, sep2 := range s {
      if sep2 == sep {
         n--
         if n == 0 {
            return s[:i], s[i+1:]
         }
      }
   }
   return s, ""
}

答案2

得分: 1

我有一个字符串 a_b_c_d_e,我想将它分割为 a_bc_d_e


strings 包中有一个 Cut 函数

// Cut 函数将字符串 s 在第一个实例 sep 处切割,
// 返回 sep 前面和后面的文本。
// found 表示 sep 是否出现在 s 中。
// 如果 sep 不在 s 中,Cut 返回 s, "", false。

将这个函数分叉为 Cut2 函数

package main

import (
	"fmt"
	"strings"
)

// Cut2 函数将字符串 s 在第二个实例 sep 处切割,
// 返回 sep 前面和后面的文本。
// found 表示 sep 是否出现两次在 s 中。
// 如果 sep 不在 s 中出现两次,Cut2 返回 s, "", false。
func Cut2(s, sep string) (before, after string, found bool) {
	if i := strings.Index(s, sep); i >= 0 {
		i += len(sep)
		if j := strings.Index(s[i:], sep); j >= 0 {
			i += j
			return s[:i], s[i+len(sep):], true
		}
	}
	return s, "", false
}

func main() {
	s := "a_b_c_d_e"
	fmt.Println(s)
	fmt.Println(Cut2(s, "_"))
}

https://go.dev/play/p/6-OBBU70snQ

a_b_c_d_e
a_b c_d_e true
英文:

> I have a string a_b_c_d_e. I want to split it as a_b and c_d_e.


In the strings package there is a Cut function

// 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, "", false.

Fork the code for the Cut function as Cut2

package main

import (
	"fmt"
	"strings"
)

// Cut2 slices s around the second instance of sep,
// returning the text before and after sep.
// The found result reports whether sep appears twice in s.
// If sep does not appear twice in s, Cut2 returns s, "", false.
func Cut2(s, sep string) (before, after string, found bool) {
	if i := strings.Index(s, sep); i >= 0 {
		i += len(sep)
		if j := strings.Index(s[i:], sep); j >= 0 {
			i += j
			return s[:i], s[i+len(sep):], true
		}
	}
	return s, "", false
}

func main() {
	s := "a_b_c_d_e"
	fmt.Println(s)
	fmt.Println(Cut2(s, "_"))
}

https://go.dev/play/p/6-OBBU70snQ

a_b_c_d_e
a_b c_d_e true

huangapple
  • 本文由 发表于 2022年10月2日 20:02:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/73925647.html
匿名

发表评论

匿名网友

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

确定