SHARE
TWEET

Untitled

a guest Aug 21st, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const getMbStringLength = str => {
  2.   let totalLength = 0;
  3.   const list = str.split('');
  4.   for (let i = 0, len = list.length; i < len; i++) {
  5.     const s = list[i];
  6.     if (s.match(/[\u0000-\u00ff]/g)) {
  7.       //半角
  8.       totalLength += 1;
  9.     } else if (s.match(/[\u4e00-\u9fa5]/g)) {
  10.       //中文
  11.       totalLength += 2;
  12.     } else if (s.match(/[\uff00-\uffff]/g)) {
  13.       //全角
  14.       totalLength += 2;
  15.     } else {
  16.       // 其它按全角处理
  17.       totalLength += 2;
  18.     }
  19.   }
  20.   return totalLength;
  21. };
  22.  
  23. export const splitMbString = (str, start = 0, end) => {
  24.   let totalLength = 0;
  25.   const list = str.split('');
  26.   for (let i = 0, len = list.length; i < len; i++) {
  27.     const s = list[i];
  28.     if (s.match(/[\u0000-\u00ff]/g)) {
  29.       //半角
  30.       totalLength += 1;
  31.     } else if (s.match(/[\u4e00-\u9fa5]/g)) {
  32.       //中文
  33.       totalLength += 2;
  34.     } else if (s.match(/[\uff00-\uffff]/g)) {
  35.       //全角
  36.       totalLength += 2;
  37.     } else {
  38.       totalLength += 2;
  39.     }
  40.  
  41.     if (totalLength >= end) {
  42.       return [
  43.         list.slice(start, i).join(''),
  44.         list.slice(i, list.length).join(''),
  45.       ];
  46.     }
  47.   }
  48. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top