refactor: 在 SplitN 函数中添加字符串修剪功能,去除文本开头的空格,以增强分割功能的准确性

This commit is contained in:
lixiangwuxian 2025-07-17 14:55:59 +08:00
parent c434a3f0ce
commit 29f901a565

View File

@ -2,6 +2,7 @@ package util
import ( import (
"regexp" "regexp"
"strings"
) )
/* /*
@ -11,6 +12,7 @@ import (
@return 分割后的字符串数组 @return 分割后的字符串数组
*/ */
func SplitN(text string, n int) []string { func SplitN(text string, n int) []string {
text = strings.TrimLeft(text, " ")
re := regexp.MustCompile(`\s+`) re := regexp.MustCompile(`\s+`)
tokens := re.Split(text, n) tokens := re.Split(text, n)
return tokens return tokens