Skip to content

cutCHSString

截取中文字符串,中文算 2 个字节。

用法

js
import { cutCHSString } from 'js-cool'

签名

typescript
function cutCHSString(str: string, len?: number, hasDot?: boolean): string

参数

参数类型描述
strstring要截取的字符串
lennumber最大长度(默认:字符串长度)
hasDotboolean截断时是否添加省略号(默认:false)

返回值

string - 截取后的字符串。

示例

js
cutCHSString('Hello世界', 7) // 'Hello世'
cutCHSString('Hello世界你好', 8, true) // 'Hello世...'
cutCHSString('中文测试字符串', 6) // '中文测试'

基于 MIT 许可发布