go的字符串处理之分隔

---疑问---

	str = "httpsh://"
	fmt.Println("???",strings.TrimLeft(str, "https"))  // 输出:// 而不是h://
package main

import (
	"fmt"
	"strings"
)

func main() {
	var str1 string
	str := "  ismdeep  \n "
	fmt.Printf("00>> str = {%s}\n %q \n", str,str)

	str1 = strings.TrimSpace(str)
	fmt.Printf("01>> str1 = {%s}\n", str1)

	str2 := strings.Trim(str, " \np")
	fmt.Printf("02>> str2 = {%s}\n", str2)

	str3 := strings.TrimLeft(str, " is")
	fmt.Printf("03>> str3 = {%s}\n", str3)

	str4 := strings.TrimRight(str, "\n ep ")
	fmt.Printf("04>> str4 = {%s}\n", str4)

	s := "Hello world hello world"
	str0 := "world"
	ret := strings.TrimRight(s,str0)
	fmt.Println("ret>>",ret) // Hello world hello

	ret1 := strings.TrimLeft(s,"He")
	fmt.Println("ret1>>",ret1) // Hello world hello
	//string.TrimLeft();  //将字符串最前面的空格修整掉。当在没有参数的情况下调用时,TrimLeft删除换行符,空格和tab字符。
	//string.TrimRight();//消除从右侧起所遇到的所有空格字符


	str = "httpshtpa://"
	fmt.Println("???",strings.TrimLeft(str, "https"))  // 输出:// 而不是h://

	str = "  hello world.   "
	fmt.Println(strings.Fields(str))
	fmt.Printf(">>%v\n", strings.Fields(str))

	str = "a|b|c"
	fmt.Println(strings.Split(str, "|"))
	fmt.Println(strings.Join(strings.Split(str, "|"), "/"))
}
package main

import (
	"fmt"
	"strings"
)

func main(){
	str := "  hello world abc \n  "
	result := strings.Replace(str,"world", "you",1)
	fmt.Println("replace:", result)
	
	count := strings.Count(str,"l")
	fmt.Println( "count: ", count)
	
	result = strings.Repeat(str,3)
	fmt.Println("repeat:", result)

	result = strings.ToUpper(str)
	fmt.Println("upper:", result)

	result = strings.ToLower(result)
	fmt.Println("lower: ", result)
	
	result = strings.TrimSpace(str)
	fmt.Printf("trimSpace:{%s}\n", result)
	
	result = strings.Trim(str, " \n\r")
	fmt.Printf("trim:{%s}\n", result)
	
	result = strings.TrimLeft(str, "    \n\r" )
	fmt.Printf("trimLeft:{%s}\n", result)
	
	result = strings.TrimRight(str, "    \n\r")
	fmt.Printf("trimRight:{%s}\n", result)
}

posted @ 2022-03-04 23:47  ty1539  阅读(237)  评论(0编辑  收藏  举报