function substrByByte (str, maxLength) => {
if (!str || !maxLength) {
return ''
}
let result = ''
let temp = str.replace(/[^\x00-\xff]/g, "**")
let temp1 = temp.substring(0, maxLength)
let x_length = temp1.split("\*").length - 1
let hanzi_num = x_length / 2
maxLength = maxLength - hanzi_num
let res = str.substring(0,maxLength)
if (maxLength < str.length) {
result = res + '...'
} else {
result = res
}
let len = temp.length
if (len > maxLength) {
return result
} else {
return str
}
}
Javascript按照字符数截取字符串
最新推荐文章于 2023-10-26 13:17:47 发布