prepare:
获得word.js 中的超长字符串,word.js 地址:汉字拼音首字母unicode 顺序排列序列
how to do:
import {strChinestFirstPy} from './word';function getFirstLetter(str) {
if (typeof str !== 'string') {
str = String(str);
}
str = str.trim();
let uni = str.charCodeAt(0);
if (uni > 40869 || uni < 19968) {
return str.charAt(0);
}
return strChineseFirstPY.charAt(uni - 19968);
}tips:
1. String.prototype.trim(): 移除字符串的首尾空格,不会改变原字符串
正则实现:
function myTrim(x) {
return x.replace(/^\s+|\s+$/gm,'');
}2. String.prototype.charCodeAt(index): charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.
3. String.prototype.charAt(index): charAt() 方法从一个字符串中返回指定的字符, 如果没有提供索引,charAt() 将使用0。