实现Demo

package main

import "fmt"

func DeletePreAndSufSpace(str string) string {
	strList := []byte(str)
	spaceCount, count := 0, len(strList)
	for i := 0; i <= len(strList)-1; i++ {
		if strList[i] == 32 {
			spaceCount++
		} else {
			break
		}
	}

	strList = strList[spaceCount:]
	spaceCount, count = 0, len(strList)
	for i := count - 1; i >= 0; i-- {
		if strList[i] == 32 {
			spaceCount++
		} else {
			break
		}
	}

	return string(strList[:count-spaceCount])
}

func main() {
	str := " 1111    "
	s := DeletePreAndSufSpace(str)
	fmt.Println(len(s))
}

输出

4

UPDATE AT 2020-5-19 09:31:42

可以直接使用strings包提供的函数

实现Demo

func main() {
	str := " 1111    "
	s := strings.Trim(str," ")
	fmt.Println(len(s))
}

输出:

4

相关文章:

  • 2021-10-15
  • 2022-12-23
  • 2021-08-13
猜你喜欢
  • 2022-03-10
  • 2021-07-14
  • 2021-08-15
  • 1970-01-01
  • 2021-10-05
  • 2021-12-20
相关资源
相似解决方案