如何从字符串中删除二进制内容?

huangapple go评论82阅读模式
英文:

How to remove binary content from the strings?

问题

我正在尝试使用golang从mp3文件中读取ID3标签。我得到了结果。但是根据我的数据库查看器显示,它们还包含一些二进制内容。

有没有办法在golang中去除这些内容?

英文:

I was trying to read ID3 tags from mp3 files using golang.
I get the results. But they also contain some binary content as my database viewer suggests.

如何从字符串中删除二进制内容?

Any way to remove that content in golang?

答案1

得分: 0

这是id3-go中的一个当前问题:PR 8修复了id3v1的问题。

但对于id3v2来说,这还不够,如此提交所示,它确实需要修剪这些空字符。
请看cutset := string(rune(0))的用法,以及TrimRight(s string, cutset string)的用法,例如strings.TrimRight(fd.Title(), cutset)

fd, err := id3.Open(path)
if err != nil {
	item.Title = f.Name()
} else {
	defer fd.Close()
	cutset := string(rune(0))
	title := strings.TrimRight(fd.Title(), cutset)
	author := strings.TrimRight(fd.Artist(), cutset)
	if len(title) > 0 {
		item.Title = title
	} else {
		item.Title = author
		if len(author) > 0 {
			item.Title += " - "
		}
		item.Title += strings.TrimRight(f.Name(), cutset)
	}
	item.Subtitle = author
	tcon := fd.Frame("TCON")
	if tcon != nil {
		item.Categories = append(item.Categories, Text{Value: strings.TrimRight(tcon.String(), cutset)})
	}
	item.PubDate = strings.TrimRight(formatYear(fd.Year()), cutset)
英文:

This is a current issue in id3-go: PR 8 fixes it for id3v1

But that isn't enough for id3v2, as show in this commit, which does have to trim those null characters.
See the use of cutset := string(rune(0)), and of TrimRight(s string, cutset string), as in for instance strings.TrimRight(fd.Title(), cutset):

fd, err := id3.Open(path)
if err != nil {
	item.Title = f.Name()
} else {
	defer fd.Close()
	cutset := string(rune(0))
	title := strings.TrimRight(fd.Title(), cutset)
	author := strings.TrimRight(fd.Artist(), cutset)
	if len(title) > 0 {
		item.Title = title
	} else {
		item.Title = author
		if len(author) > 0 {
			item.Title += " - "
		}
		item.Title += strings.TrimRight(f.Name(), cutset)
	}
	item.Subtitle = author
	tcon := fd.Frame("TCON")
	if tcon != nil {
		item.Categories = append(item.Categories, Text{Value: strings.TrimRight(tcon.String(), cutset)})
	}
	item.PubDate = strings.TrimRight(formatYear(fd.Year()), cutset)

huangapple
  • 本文由 发表于 2015年11月7日 12:01:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/33578992.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定