pinyincode

package module
v0.0.0-...-079e056 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2022 License: MIT Imports: 1 Imported by: 0

README

Pinyincode

pinyincode 用于将包含中文的字符串转为拼音码

比如将 中文 转为 zw,将 拼音码 转为 pym

Installation

go get -u github.com/koulerz/pinyincode

Warning ⚠️

  • 不能够保证多音字的拼音码符合预期。比如 重庆 的拼音码可能为 cq 或 zq

Usage

package main

import (
	"fmt"

	"github.com/koulerz/pinyincode"
)

func main() {
	// 保留英文字母和数字
	// output: sjnhHelloWorld
	fmt.Println(pinyincode.Code("世界-你好!Hello_World!", pinyincode.FlagWithEnglish|pinyincode.FlagWithNumber))

	// 保留英文字母,数字,连字符和下划线
	// output: sj-nhHello_World
	fmt.Println(pinyincode.Code("世界-你好!Hello_World!", pinyincode.FlagWithEnglish|pinyincode.FlagWithNumber|pinyincode.FlagWithHyphen|pinyincode.FlagWithUnderscore))

	// 仅保留中文拼音
	// output: sjnh
	fmt.Println(pinyincode.Code("世界-你好!Hello_World!", pinyincode.FlagOnlyPinyin))

	// 仅保留中文拼音,FlagOnlyPinyin 会使其他 Flag 失效
	// output: sjnh
	fmt.Println(pinyincode.Code("世界-你好!Hello_World!", pinyincode.FlagOnlyPinyin|pinyincode.FlagWithEnglish|pinyincode.FlagWithNumber))
}

Flags

Flag Description
FlagWithEnglish 保留大小写英文字母
FlagWithNumber 保留数字
FlagWithHyphen 保留连字符
FlagWithUnderscore 保留下划线
FlagOnlyPinyin 仅保留中文拼音。该 Flag 会使其他 Flag 失效

Dependencies

License

Under the MIT License.

Documentation

Index

Constants

View Source
const (
	FlagWithEnglish    = 0b0000_0001 // 保留英文字母
	FlagWithNumber     = 0b0000_0010 // 保留数字
	FlagWithHyphen     = 0b0000_0100 // 保留连字符
	FlagWithUnderscore = 0b0000_1000 // 保留下划线

	FlagOnlyPinyin = 0b1111_1111 // 仅保留中文拼音
)

Variables

This section is empty.

Functions

func Code

func Code(hans string, flag int) string

Code 获取拼音码,通过 flag 自定义保留的字符

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL