This article summarizes some common methods for string, most of which are used based on js. Recently I have summarized some common methods in string,
Most of the methods are from the JavaScript Framework Design book,
If there are better methods or other commonly used methods for string, I hope you will not be enlightened.
Part 1:
String search,
Including whether a string is inside another string, whether it is at the beginning, and whether it is at the end.
/*** Determine whether a string contains another string * @ param target * @ param it * @ returns {boolean} */function contains (target, it) {return target. indexOf (it )! =-1 ;} /*** determine whether the target string is located at the beginning of the original string * @ param target * @ param str * @ param ignoreCase whether case sensitivity is ignored * @ returns {boolean} */function startWith (target, str, ignoreCase) {const startStr = target. substr (0, str. length); return ignoreCase? StartStr. toLocaleLowerCase () = str. toLocaleLowerCase (): startStr = str ;} /*** determine whether the target string is located at the end of the original string * @ param target * @ param str * @ param ignoreCase whether case sensitivity is ignored * @ returns {boolean} */function endWith (target, str, ignoreCase) {const startStr = target. substr (target. length-str. length); return ignoreCase? StartStr. toLocaleLowerCase () === str. toLocaleLowerCase (): startStr = str ;}
Part 2:
Duplicate string,
Returns a string that is repeated n times.
/*** Repeated string ** @ param target string * @ param n times * @ returns {string} */function repeat (target, n) {return (new Array (n + 1 )). join (target);} function repeat2 (target, n) {return Array. prototype. join. call ({length: n + 1}, target);} function repeat3 (target, n) {let s = target, total = []; while (n> 0) {if (n % 2 = 1) {// if it is an odd total [total. length] = s;} if (n = 1) {break;} s + = s; // divide n by 2 to obtain the operator, or describe the quadratic n = n> 1;} return total. join ('');} function repeat4 (target, n) {if (n = 1) {return target;} let s = this. repeat4 (target, Math. floor (n/2); s + = s; if (n % 2) {s + = target;} return s;} function repeat5 (target, n) {let s = target, c = s. length * n; do {s + = s;} while (n = n> 1); // This eliminates the need to judge the parity s = s. substring (0, c); return s ;} /*** best score for each browser * @ param target * @ param n * @ returns {string} */function repeat6 (target, n) {let s = target, total = ''; while (n> 0) {if (n % 2 = 1) {total + = s;} if (n = 1) {break;} s + = s; n = n> 1;} return total ;}
Part 3:
String Length,
Obtains the length of a string in bytes and supports the storage of custom Chinese characters.
/*** Get the length of all bytes of a string * @ param target * @ returns {number} */function byteLen (target) {let byteLength = target. length, I = 0; for (; I <target. length; I ++) {if (target. charCodeAt (I)> 255) {byteLength ++;} return byteLength;}/*** use regular expressions, the storage Byte Count of custom Chinese characters * @ param target * @ param fix * @ returns {Number} */function byteLen2 (target, fix) {fix = fix? Fix: 2; const str = new Array (fix + 1 ). join ('-'); return target. replace (/[^ \ x00-\ xff]/g, str ). length ;}
Part 4:
String replacement,
Including string truncation and replacement, removing some html tags, and replacing HTML unsafe tags in the string
/*** Truncates a string. If the length exceeds the limit, by default, three dots or other * @ param target * @ param length * @ param truncation * @ returns {string} */function truncate (target, length, truncation) are added) {length = length | 30; truncation = void (0 )? '...': Truncation; return target. length> length? Target. slice (0, length-truncation. length) + truncation: String (target);}/*** remove the HTML Tag * @ param target * @ returns {string} */function stripTags (target) from the String) {return String (target | ''). replace (/<[^>] +>/g ,'');} /*** remove all script tags in the string * @ param target * @ returns {String} */function stripScripts (target) {return string (target | ''). replace (/
] *> ([\ S \ s *?]) <\/Script>/img ,'');} /*** escape the string through HTML to get the content suitable for display on the page * @ param target * @ returns {string | XML} */function escapeHTML (target) {return target. replace (// g ,'&'). replace (//G, '>'). replace (/''/g, '"'). replace (/'/g ,''');}
Part 5:
String filling,
Fill a specific string in another string to get the corresponding length of the target string, including filling forward and backward.
/*** Obtain n digits. If not, add 0 * @ param target * @ param n digits * @ returns {string} */function pad (target, n) {const zero = new Array (n ). join ('0'); const str = zero + target; return str. substr (-n);} function pad2 (target, n) {let len = target. toString (). length; while (len <n) {target = '0' + target; len ++;} return target;}/*** multiple types of filling are supported, you can also select the filling position (front or back) * @ param target * @ param n * @ param filling * @ param right * @ Param radix * @ returns {string} */function pad8 (target, n, filling, right, radix) {var num = target. toString (radix | 10); filling = filling | '0'; while (num. length <n) {if (! Right) {num = filling + num;} else {num + = filling;} return num ;}
Part 6:
Removes spaces before and after a string,
/*** Remove spaces before and after the string * @ param str * @ returns {string | XML} */function trim (str) {return str. replace (/^ \ s */,''). replace (/\ s * $/, '');} function trim2 (str) {return str. replace (/^ \ s + /,''). replace (/\ s + $/, '');} function trim3 (str) {return str. substring (Math. max (str. search (/\ S/), 0), str. search (/\ S \ s * $/) + 1);} function trim4 (str) {return str. replace (/^ \ s + | \ s + $/g, '');} function trim5 (str) {str = str. match (/ \ S + (? : \ S + \ S +) */); return str? Str [0]: '';} function trim6 (str) {return str. replace (/^ \ s * (\ S * (\ s \ S +) *) \ s * $/, '$ 1');} function trim7 (str) {return str. replace (/^ \ s * (\ S *(? : \ S + \ S +) *) \ s * $/, '$ 1');} function trim8 (str) {return str. replace (/^ \ s *((? : [\ S \ s] * \ S )?) \ S * $/, '$1 ');}
The above is a detailed description of some common methods of string in js. For more information, see other related articles in the first PHP community!