| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
cnaw1986
8年前发布

Golang 字符串操作小结

   <p>本文形式为, 给出一个函数接口, 然后匹配相关example. 关注函数主要集中在 strings 和 strconv</p>    <h2>字符串比较</h2>    <p>函数接口</p>    <pre>  <code class="language-go">// Compare比较字符串的速度比字符串内建的比较要快  funcCompare(a, bstring)int  </code></pre>    <pre>  <code class="language-go">fmt.Println(strings.Compare(string("hello"),string("haha")))// 1  fmt.Println(strings.Compare(string("hello"),string("world")))// -1  fmt.Println(strings.Compare(string("hello"),string("helloworld")))// -1  fmt.Println(strings.Compare(string("hello"),string("hello")))//0  </code></pre>    <h2>字符串查找</h2>    <p>字符串查找的相关接口</p>    <pre>  <code class="language-go">// 判断给定字符串s中是否包含子串substr, 找到返回true, 找不到返回false  funcContains(s, substrstring)bool    // 在字符串s中查找sep所在的位置, 返回位置值, 找不到返回-1  funcIndex(s, sepstring)int    // 统计给定子串sep的出现次数, sep为空时, 返回1 + 字符串的长度  funcCount(s, sepstring)int  </code></pre>    <pre>  <code class="language-go">fmt.Println(strings.Contains("seafood","foo"))// true  fmt.Println(strings.Contains("seafood","bar"))// false  fmt.Println(strings.Contains("seafood",""))// true  fmt.Println(strings.Contains("",""))// true    fmt.Println(strings.Index("chicken","ken"))// 4  fmt.Println(strings.Index("chicken","dmr"))// -1      fmt.Println(strings.Count("cheeseeee","ee"))// 3  fmt.Println(strings.Count("five",""))// 5  </code></pre>    <h2>字符串重复</h2>    <pre>  <code class="language-go">// 重复s字符串count次, 最后返回新生成的重复的字符串  func Repeat(sstring,countint)string  </code></pre>    <pre>  <code class="language-go">fmt.Println("ba"+ strings.Repeat("na",2))// banana  </code></pre>    <h2>字符串替换</h2>    <pre>  <code class="language-go">// 在s字符串中, 把old字符串替换为new字符串,n表示替换的次数,小于0表示全部替换  funcReplace(s, old,newstring, nint)string  </code></pre>    <pre>  <code class="language-go">fmt.Println(strings.Replace("oink oink oink","k","ky",2))// oinky oinky oink  fmt.Println(strings.Replace("oink oink oink","oink","moo", -1))// moo moo moo  </code></pre>    <h2>字符串删除</h2>    <pre>  <code class="language-go">// 删除在s字符串的头部和尾部中由cutset指定的字符, 并返回删除后的字符串  funcTrim(sstring, cutsetstring)string    // 删除首尾的空格  </code></pre>    <pre>  <code class="language-go">// 删除首部和尾部的 ! 和空格  fmt.Printf("%q\n", strings.Trim(" !!! Achtung! Achtung! !!! ","! ")) //"Achtung! Achtung"    fmt.Printf("%q\n", strings.TrimSpace(" \t\n a lone gopher \n\t\r\n")) //"a lone gopher"  </code></pre>    <h2>字符串大小写转换</h2>    <pre>  <code class="language-go">// 给定字符串转换为英文标题的首字母大写的格式(不能正确处理unicode标点)  funcTitle(sstring)string    // 所有字母转换为小写  funcToLower(sstring)string    // 所有字母转换为大写  funcToUpper(sstring)string  </code></pre>    <pre>  <code class="language-go">fmt.Println(strings.Title("her royal highness"))// Her Royal Highness    fmt.Println(strings.ToLower("Gopher123"))// gopher123    fmt.Println(strings.ToUpper("Gopher"))// GOPHER  </code></pre>    <h2>字符串前缀后缀</h2>    <p>前缀和后缀的判断均为大小写敏感</p>    <pre>  <code class="language-go">// 判断字符串是否包含前缀prefix  funcHasPrefix(s, prefixstring)bool    // 判断字符串是否包含后缀suffix,   funcHasSuffix(s, suffixstring)bool  </code></pre>    <pre>  <code class="language-go">fmt.Println(strings.HasPrefix("Gopher","Go"))// true  fmt.Println(strings.HasPrefix("Gopher","go"))// false  fmt.Println(strings.HasPrefix("Gopher","C"))// false  fmt.Println(strings.HasPrefix("Gopher",""))// true    fmt.Println(strings.HasSuffix("Amigo","go"))// true  fmt.Println(strings.HasSuffix("Amigo","O"))// false  fmt.Println(strings.HasSuffix("Amigo","Ami"))// false  fmt.Println(strings.HasSuffix("Amigo",""))// true  </code></pre>    <h2>字符串分割</h2>    <p>函数接口</p>    <pre>  <code class="language-go">// 把字符串按照sep进行分割, 返回slice(类似于python中的split)  funcSplit(s, sepstring) []string    // 去除字符串s中的空格符, 并按照空格(可以是一个或者多个空格)分割字符串, 返回slice  funcFields(sstring) []string    // 当字符串中字符c满足函数f(c)时, 就进行字符串s的分割  funcFieldsFunc(sstring, ffunc(rune)bool) []string  </code></pre>    <p>使用example</p>    <pre>  <code class="language-go">func main() {   fmt.Printf("%q\n", strings.Split("a,b,c",",")) // ["a""b""c"]   fmt.Printf("%q\n", strings.Split("a man a plan a canal panama","a ")) // ["""man ""plan ""canal panama"]   fmt.Printf("%q\n", strings.Split(" xyz ","")) // [" ""x""y""z"" "]   fmt.Printf("%q\n", strings.Split("","Bernardo O'Higgins")) // [""]   fmt.Printf("%q\n", strings.Split("1 og.txt"," "))  } // ["1""og.txt"]     fmt.Printf("Fields are: %q\n", strings.Fields(" foo bar baz ")) //Fieldsare: ["foo""bar""baz"]     f := func(c rune) bool{  return!unicode.IsLetter(c) && !unicode.IsNumber(c)   }   // 表示按照非字母, 非数字来分割字符串   fmt.Printf("Fields are: %q\n", strings.FieldsFunc(" foo1;bar2,baz3...", f)) //Fieldsare: ["foo1""bar2""baz3"]  </code></pre>    <h2>字符串拼接</h2>    <p>三种拼接方案:</p>    <ul>     <li>直接用 += 操作符, 直接将多个字符串拼接. 最直观的方法, 不过当数据量非常大时用这种拼接访求是非常低效的.</li>     <li>用字符串切片([]string)装载所有要拼接的字符串,最后使用strings.Join() 函数一次性将所有字符串拼接起来。在数据量非常大时,这种方法的效率也还可以的。</li>     <li>利用Buffer( Buffer是一个实现了读写方法的可变大小的字节缓冲 ),将所有的字符串都写入到一个Buffer变量中,最后再统一输出.</li>    </ul>    <p>函数接口</p>    <pre>  <code class="language-go">// bytes.Buffer的方法, 将给定字符串追加(append)到Buffer  func(b *Buffer) WriteString(sstring) (nint, err error)    // 字符串拼接, 把slice通过给定的sep连接成一个字符串  funcJoin(a []string, sepstring)string  </code></pre>    <p>三种字符串拼接方式的性能测试(出自参考链接的文章)</p>    <pre>  <code class="language-go">package main    import (  "bytes"  "fmt"  "strings"  "time"  )    func main() {   var buffer bytes.Buffer     s := time.Now()  fori :=0; i <100000; i++ {   buffer.WriteString("test is here\n")   }   buffer.String() // 拼接结果   e := time.Now()   fmt.Println("taked time is ", e.Sub(s).Seconds())     s = time.Now()   str := ""  fori :=0; i <100000; i++ {   str += "test is here\n"   }   e = time.Now()   fmt.Println("taked time is ", e.Sub(s).Seconds())     s = time.Now()   var sl []string  fori :=0; i <100000; i++ {   sl = append(sl, "test is here\n")   }   strings.Join(sl, "")   e = time.Now()   fmt.Println("taked time is", e.Sub(s).Seconds())  }  </code></pre>    <p>运行结果如下</p>    <pre>  <code class="language-go">taked timeis0.004145088  taked timeis13.78821647  taked timeis0.024005696  </code></pre>    <h2>字符串转换</h2>    <p>字符串转化的函数在 strconv 中</p>    <ul>     <li>Append* 函数表示将给定的类型(如 bool, int 等)转换为字符串后, 添加在现有的字节数组中 []byte</li>     <li>Format* 函数将给定的类型变量转换为string返回</li>     <li>Parse* 函数将字符串转换为其他类型</li>    </ul>    <pre>  <code class="language-go">// 字符串转整数  funcAtoi(sstring) (iint, err error)  // 整数值换字符串  funcItoa(iint)string  </code></pre>    <pre>  <code class="language-go">str:= make([]byte,0,100)  str= strconv.AppendInt(str,123,10)// 10用来表示进制, 此处为10进制  str= strconv.AppendBool(str,false)  str= strconv.AppendQuote(str,"andrew")  str= strconv.AppendQuoteRune(str,'刘')  fmt.Println(string(str))// 123false"andrew"'刘'    s := strconv.FormatBool(true)  fmt.Printf("%T, %v\n", s, s)// string, true  v := 3.1415926535  s32 := strconv.FormatFloat(v, 'E', -1,32)  fmt.Printf("%T, %v\n", s32, s32)// string, 3.1415927E+00  s10 := strconv.FormatInt(-44,10)  fmt.Printf("%T, %v\n", s10, s10)// string, -44      num := strconv.Itoa(1234)  fmt.Printf("%T, %v\n", s, s)// int, 1023  fmt.Printf("%T, %v\n", num, num)// string, 1234  </code></pre>    <h2>参考链接</h2>    <ul>     <li><a href="/misc/goto?guid=4959671858036593502" rel="nofollow,noindex">go语言中高效字符串拼接</a></li>     <li><a href="/misc/goto?guid=4959671858128857145" rel="nofollow,noindex">7.6 字符串处理</a></li>    </ul>    <p>来自: <a href="/misc/goto?guid=4959671858222964271" rel="nofollow">http://andrewliu.in/2016/04/29/Golang-字符串操作小结/</a> </p>    
 本文由用户 cnaw1986 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1461899459404.html
Go语言 Google Go/Golang开发