golang截取字符串

前端之家收集整理的这篇文章主要介绍了golang截取字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

golang截取字符串

转自:http://blog.csdn.net/u012210379/article/details/44749743

package utils

func Substr(str string,start int,length int) string {
	rs := []rune(str)
	rl := len(rs)
	end := 0

	if start < 0 {
		start = rl - 1 + start
	}
	end = start + length

	if start > end {
		start,end = end,start
	}

	if start < 0 {
		start = 0
	}
	if start > rl {
		start = rl
	}
	if end < 0 {
		end = 0
	}
	if end > rl {
		end = rl
	}

	return string(rs[start:end])
}

//截取字符串 start 起点下标 end 终点下标(不包括)
func Substr2(str string,end int) string {
	rs := []rune(str)
	length := len(rs)

	if start < 0 || start > length {
		panic("start is wrong")
	}

	if end < 0 || end > length {
		panic("end is wrong")
	}

	return string(rs[start:end])
}
原文链接:https://www.f2er.com/go/188366.html

猜你在找的Go相关文章