Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export const getMbStringLength = str => {
- let totalLength = 0;
- const list = str.split('');
- for (let i = 0, len = list.length; i < len; i++) {
- const s = list[i];
- if (s.match(/[\u0000-\u00ff]/g)) {
- //半角
- totalLength += 1;
- } else if (s.match(/[\u4e00-\u9fa5]/g)) {
- //中文
- totalLength += 2;
- } else if (s.match(/[\uff00-\uffff]/g)) {
- //全角
- totalLength += 2;
- } else {
- // 其它按全角处理
- totalLength += 2;
- }
- }
- return totalLength;
- };
- export const splitMbString = (str, start = 0, end) => {
- let totalLength = 0;
- const list = str.split('');
- for (let i = 0, len = list.length; i < len; i++) {
- const s = list[i];
- if (s.match(/[\u0000-\u00ff]/g)) {
- //半角
- totalLength += 1;
- } else if (s.match(/[\u4e00-\u9fa5]/g)) {
- //中文
- totalLength += 2;
- } else if (s.match(/[\uff00-\uffff]/g)) {
- //全角
- totalLength += 2;
- } else {
- totalLength += 2;
- }
- if (totalLength >= end) {
- return [
- list.slice(start, i).join(''),
- list.slice(i, list.length).join(''),
- ];
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement