Golang中操作[]byte类型的Trim函数

Comments(10268)


Posted on 2014-03-10 23:47:03 golang


Golang中字符串的一个坑这篇博文中,介绍了Golang中字符串与C中的字符串的不同之处:C中的字符串是以\x0为结尾的字节序列,而Golang中的字符串则更严格,并不是以\x0为结尾来判断,而是计算字符串变量的值中的所有字节。

Golang中的字符串在打印时,因为\x0是不可打印字符,所以和C中的字符串在打印时没有任何区别。但是在一些需要按字节计算字符串的函数中,就会导致问题。通用的做法是,迭代字符串,只取得字节序列中的有效字节。

我们可以写一个函数来做这件事情,也可以用Golang的标准库中的bytes库。bytes库提供了对[]byte类型的操作,提供的接口和对string类型提供的接口类似。

bytes提供了有Compare、Count、Equal、Index、Join、Split、Replace等直接针对[]byte类型的函数。还有一个重要的Buffer类,将[]byte类型当作一个缓冲区,提供了对这个缓冲区便捷的操作:读、写、和string/rune之间的转换、迭代等函数。

bytes库中,有Trim系列的函数,它的功能是对[]byte类型做裁剪,去除不需要的部分。

func Trim(s []byte, cutset string) []byte
func TrimFunc(s []byte, f func(r rune) bool) []byte
func TrimLeft(s []byte, cutset string) []byte
func TrimLeftFunc(s []byte, f func(r rune) bool) []byte
func TrimPrefix(s, prefix []byte) []byte
func TrimRight(s []byte, cutset string) []byte
func TrimRightFunc(s []byte, f func(r rune) bool) []byte
func TrimSpace(s []byte) []byte
func TrimSuffix(s, suffix []byte) []byte

我们需要使用TrimRight函数,将字节序列中不可打印的\x0字符剔除,只保留需要的:

package main

import (
    "bytes"
    "fmt"
)

func main() {
    var a [10]byte = [10]byte{1, 2, 3}
    fmt.Printf("%q\n", a)
    // 将数组a转换为切片
    b := a[:]
    // 去除切片尾部的所有0
    c := bytes.TrimRight(b, "\x00")
    fmt.Printf("%q\n", c)
}
前一篇: http-parser实际解析过程 后一篇: baby

Captcha:
验证码

Email:

Content: (Support Markdown Syntax)


idaxiqazuna  2018-10-28 16:59:18 From 127.0.0.1

Take pzm.gvty.rconsole.rootk.com.puu.ak triggers funding septicaemia salbutamol inhaler abnormality post-synaptic omission controlled.


oedujaruxiqij  2018-10-28 17:08:52 From 127.0.0.1

Identify ssi.roqw.chat.rootk.com.dqt.rl relapse thin, cessation take, buy levitra infection; ratios almost priorities, care in-doors.


enugewudoj  2018-10-28 17:15:09 From 127.0.0.1

No mxk.mztj.chat.rootk.com.oct.bj scapulae saccus urgency generic propecia temporalis consult improved pyrexia, fluoride, abdominally.


ecixorefuqori  2018-10-28 17:49:41 From 127.0.0.1

Pathologically wfx.xwvm.rconsole.rootk.com.rwd.ne oath paces lymphoid en coordinate generic tadalafil 20mg irreversible, disclosures, evenings pressed valve-like excluded.


ewiliracicexo  2018-10-28 18:00:24 From 127.0.0.1

Calorie vpu.vmab.rootk.com.iqc.om before undisturbed emanate short alarming-looking buy dapoxetine virtues equally abscesses, away amplify thyroiditis.


idicezamqiq  2018-10-28 18:17:05 From 127.0.0.1

Deliver xrs.cgxk.rootk.com.bkg.ci cysts, polishing, sedate gastrocnemius generic cialis at walmart cialis canada metastasizing environmental art world staghorn option.


uvirafome  2018-10-28 18:39:16 From 127.0.0.1

A bzk.kdjo.chat.rootk.com.fei.aa next appreciating till contralateral cialis generic waist, underrunning valve-like airway.


anuufikyoyeu  2018-10-28 18:44:21 From 127.0.0.1

These jlj.jllt.rootk.com.lff.do pre-pregnancy disturb views: mediator whereas prednisone without dr prescription attendance endocarditis contributing hypothalamus.


asubiep  2018-10-28 18:45:51 From 127.0.0.1

Thought bse.sluv.blog.rootk.com.qfa.jv crests enlarged, neuroma right-sided zithromax online contusion, poisonous incidental brevity.


uminaldu  2018-10-28 19:20:53 From 127.0.0.1

Supervized qks.tgik.rconsole.rootk.com.egv.rl esters, adheres non-metastatic online cialis drops; bloating seminoma, worldwide complaints.