|
|
AIWROKÈí¼þStringʵÀýÑÝʾ
- /*
- 🍎½»Á÷QQȺ711841924Ⱥһ£¬Æ»¹ûÄÚ²âȺ£¬528816639
- 🍎🔨JavaScript×Ö·û´®¹¤¾ßÀà·â×°
- 🔨ÊÊÓñ¾ÎĵµES5ϵͳ°²×¿ JavaScriptÒýÇæRhino
- */
- /**
- * ×Ö·û´®¹¤¾ßÀà·â×°
- * Ìṩ¸ü¼ÓÃæÏò¶ÔÏóµÄ×Ö·û´®²Ù×÷½Ó¿Ú
- */
- function StringUtils() {}
- /**
- * ·µ»ØÖ¸¶¨Î»ÖõÄ×Ö·û
- * @param {string} str - Ô´×Ö·û´®
- * @param {number} index - ×Ö·ûλÖÃ
- * @returns {string} Ö¸¶¨Î»ÖõÄ×Ö·û
- */
- StringUtils.prototype.charAt = function(str, index) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- if (typeof index !== 'number' || index % 1 !== 0) throw new Error('[´íÎó] ²ÎÊýindex±ØÐëÊÇÕûÊý');
- return str.charAt(index);
- };
- /**
- * ·µ»ØÖ¸¶¨Î»ÖÃ×Ö·ûµÄUnicode±àÂë
- * @param {string} str - Ô´×Ö·û´®
- * @param {number} index - ×Ö·ûλÖÃ
- * @returns {number} Unicode±àÂëÖµ
- */
- StringUtils.prototype.charCodeAt = function(str, index) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- if (typeof index !== 'number' || index % 1 !== 0) throw new Error('[´íÎó] ²ÎÊýindex±ØÐëÊÇÕûÊý');
- return str.charCodeAt(index);
- };
- /**
- * ²éÕÒ×Ó×Ö·û´®Ê״γöÏÖµÄλÖÃ
- * @param {string} str - Ô´×Ö·û´®
- * @param {string} searchValue - Òª²éÕÒµÄ×Ó×Ö·û´®
- * @param {number} [fromIndex=0] - ¿ªÊ¼²éÕÒµÄλÖÃ
- * @returns {number} Ê״γöÏÖµÄλÖã¬Î´ÕÒµ½·µ»Ø-1
- */
- StringUtils.prototype.indexOf = function(str, searchValue, fromIndex) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- if (typeof searchValue !== 'string') throw new Error('[´íÎó] ²ÎÊýsearchValue±ØÐëÊÇ×Ö·û´®');
- return str.indexOf(searchValue, fromIndex);
- };
- /**
- * ²éÕÒ×Ó×Ö·û´®×îºóÒ»´Î³öÏÖµÄλÖÃ
- * @param {string} str - Ô´×Ö·û´®
- * @param {string} searchValue - Òª²éÕÒµÄ×Ó×Ö·û´®
- * @param {number} [fromIndex=str.length] - ¿ªÊ¼²éÕÒµÄλÖÃ
- * @returns {number} ×îºóÒ»´Î³öÏÖµÄλÖã¬Î´ÕÒµ½·µ»Ø-1
- */
- StringUtils.prototype.lastIndexOf = function(str, searchValue, fromIndex) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- if (typeof searchValue !== 'string') throw new Error('[´íÎó] ²ÎÊýsearchValue±ØÐëÊÇ×Ö·û´®');
- return str.lastIndexOf(searchValue, fromIndex);
- };
- /**
- * »ñÈ¡×Ö·û´®³¤¶È
- * @param {string} str - Ô´×Ö·û´®
- * @returns {number} ×Ö·û´®³¤¶È
- */
- StringUtils.prototype.getLength = function(str) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- return str.length;
- };
- /**
- * ʹÓÃÕýÔò±í´ïʽƥÅä×Ö·û´®
- * @param {string} str - Ô´×Ö·û´®
- * @param {RegExp} regexp - ÕýÔò±í´ïʽ
- * @returns {Array|null} Æ¥Åä½á¹ûÊý×飬δƥÅä·µ»Ønull
- */
- StringUtils.prototype.match = function(str, regexp) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- if (!(regexp instanceof RegExp)) throw new Error('[´íÎó] ²ÎÊýregexp±ØÐëÊÇÕýÔò±í´ïʽ¶ÔÏó');
- return str.match(regexp);
- };
- /**
- * Ìæ»»µÚÒ»¸öÆ¥ÅäµÄ×Ó×Ö·û´®
- * @param {string} str - Ô´×Ö·û´®
- * @param {string|RegExp} searchValue - ÒªÌæ»»µÄÄÚÈÝ
- * @param {string|Function} replaceValue - Ìæ»»ÄÚÈÝ
- * @returns {string} Ìæ»»ºóµÄÐÂ×Ö·û´®
- */
- StringUtils.prototype.replace = function(str, searchValue, replaceValue) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- return str.replace(searchValue, replaceValue);
- };
- /**
- * Ìæ»»ËùÓÐÆ¥ÅäµÄ×Ó×Ö·û´®
- * @param {string} str - Ô´×Ö·û´®
- * @param {string|RegExp} searchValue - ÒªÌæ»»µÄÄÚÈÝ
- * @param {string|Function} replaceValue - Ìæ»»ÄÚÈÝ
- * @returns {string} Ìæ»»ºóµÄÐÂ×Ö·û´®
- */
- StringUtils.prototype.replaceAll = function(str, searchValue, replaceValue) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
-
- // ¼æÈݵͰ汾»·¾³£ºÈô²»Ö§³ÖreplaceAll£¬ÓÃÕýÔò/gÌæ´ú
- if (!String.prototype.replaceAll) {
- if (typeof searchValue === 'string') {
- // ×Ö·û´®×ªÕýÔò£¨×ªÒåÌØÊâ×Ö·û£©
- var escaped = searchValue.replace(/[.*+?^${}()|[\]\\]/g, '\\[ DISCUZ_CODE_0 ]amp;');
- return str.replace(new RegExp(escaped, 'g'), replaceValue);
- } else if (searchValue instanceof RegExp && !searchValue.global) {
- // ÕýÔòÎÞ/gÐÞÊηû£¬Ìí¼ÓºóÆ¥ÅäËùÓÐ
- var flags = 'g';
- if (searchValue.ignoreCase) flags += 'i';
- if (searchValue.multiline) flags += 'm';
- var newRegexp = new RegExp(searchValue.source, flags);
- return str.replace(newRegexp, replaceValue);
- }
- }
- return str.replaceAll(searchValue, replaceValue);
- };
- /**
- * ·Ö¸î×Ö·û´®ÎªÊý×é
- * @param {string} str - Ô´×Ö·û´®
- * @param {string|RegExp} separator - ·Ö¸ô·û
- * @param {number} [limit] - ÏÞÖÆ·µ»ØÊý×é´óС
- * @returns {Array} ·Ö¸îºóµÄ×Ö·û´®Êý×é
- */
- StringUtils.prototype.split = function(str, separator, limit) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- return str.split(separator, limit);
- };
- /**
- * ÅжÏ×Ö·û´®ÊÇ·ñÒÔÖ¸¶¨ÄÚÈÝ¿ªÍ·
- * @param {string} str - Ô´×Ö·û´®
- * @param {string} searchValue - Òª¼ì²âµÄÄÚÈÝ
- * @param {number} [position=0] - ¼ì²â¿ªÊ¼Î»ÖÃ
- * @returns {boolean} ÊÇ·ñÒÔÖ¸¶¨ÄÚÈÝ¿ªÍ·
- */
- StringUtils.prototype.startsWith = function(str, searchValue, position) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- if (typeof searchValue !== 'string') throw new Error('[´íÎó] ²ÎÊýsearchValue±ØÐëÊÇ×Ö·û´®');
-
- // ¼æÈݵͰ汾»·¾³£ºÊÖ¶¯ÊµÏÖstartsWith
- if (!String.prototype.startsWith) {
- var pos = position || 0;
- return str.substr(pos, searchValue.length) === searchValue;
- }
- return str.startsWith(searchValue, position);
- };
- /**
- * ´ÓÖ¸¶¨Î»ÖýØÈ¡Ö¸¶¨³¤¶ÈµÄ×Ó×Ö·û´®
- * @param {string} str - Ô´×Ö·û´®
- * @param {number} start - ¿ªÊ¼Î»ÖÃ
- * @param {number} [length] - ½ØÈ¡³¤¶È
- * @returns {string} ½ØÈ¡µÄ×Ó×Ö·û´®
- */
- StringUtils.prototype.substr = function(str, start, length) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- if (typeof start !== 'number' || start % 1 !== 0) throw new Error('[´íÎó] ²ÎÊýstart±ØÐëÊÇÕûÊý');
- return str.substr(start, length);
- };
- /**
- * ½ØÈ¡Ö¸¶¨·¶Î§µÄ×Ó×Ö·û´®
- * @param {string} str - Ô´×Ö·û´®
- * @param {number} start - ¿ªÊ¼Î»ÖÃ
- * @param {number} [end] - ½áÊøÎ»Ö㨲»°üº¬£©
- * @returns {string} ½ØÈ¡µÄ×Ó×Ö·û´®
- */
- StringUtils.prototype.substring = function(str, start, end) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- if (typeof start !== 'number' || start % 1 !== 0) throw new Error('[´íÎó] ²ÎÊýstart±ØÐëÊÇÕûÊý');
- return str.substring(start, end);
- };
- /**
- * È¥³ý×Ö·û´®Ê×β¿Õ°××Ö·û
- * @param {string} str - Ô´×Ö·û´®
- * @returns {string} È¥³ýÊ×β¿Õ°××Ö·ûºóµÄ×Ö·û´®
- */
- StringUtils.prototype.trim = function(str) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
-
- // ¼æÈݵͰ汾»·¾³£ºÊÖ¶¯ÊµÏÖtrim
- if (!String.prototype.trim) {
- return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
- }
- return str.trim();
- };
- /**
- * ת»»×Ö·û´®Îª´óд
- * @param {string} str - Ô´×Ö·û´®
- * @returns {string} ת»»ºóµÄ´óд×Ö·û´®
- */
- StringUtils.prototype.toUpperCase = function(str) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- return str.toUpperCase();
- };
- /**
- * ת»»×Ö·û´®ÎªÐ¡Ð´
- * @param {string} str - Ô´×Ö·û´®
- * @returns {string} ת»»ºóµÄСд×Ö·û´®
- */
- StringUtils.prototype.toLowerCase = function(str) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- return str.toLowerCase();
- };
- /**
- * Á¬½Ó×Ö·û´®
- * @param {string} str - Ô´×Ö·û´®
- * @param {...string} strings - ÒªÁ¬½ÓµÄ×Ö·û´®
- * @returns {string} Á¬½ÓºóµÄ×Ö·û´®
- */
- StringUtils.prototype.concat = function(str) {
- if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
- var args = Array.prototype.slice.call(arguments, 1);
- return str.concat.apply(str, args);
- };
- // ´´½¨È«¾ÖʵÀý£¬·½±ãÖ±½Óµ÷ÓÃ
- var StrUtil = new StringUtils();
- // ===============================
- // ʹÓÃʾÀý
- // ===============================
- function printl() {
- // ÊÊÅä´òÓ¡º¯Êý£º¶¨ÒåprintlÓÃÓÚÊä³ö
- var args = Array.prototype.slice.call(arguments);
-
- // Èô»·¾³Ö§³Öconsole.logÖ±½Óµ÷Óã¬Èô²»Ö§³Ö¿ÉÌæ»»ÎªÆäËûÊä³ö·½Ê½
- if (typeof console !== 'undefined' && console.log) {
- console.log.apply(console, args);
- } else {
- // ½µ¼¶´¦Àí£ºÆ´½Ó×Ö·û´®Êä³ö£¨ÊÊÅäÎÞconsole»·¾³£©
- var output = args.join(' ');
- if (typeof document !== 'undefined') {
- document.write(output + '<br>');
- } else if (typeof process !== 'undefined' && process.stdout) {
- // Node.js»òÆäËû»·¾³¶µµ×
- process.stdout.write(output + '\n');
- }
- }
- }
- printl("=== 🚀 ×Ö·û´®¹¤¾ßÀà·â×° - ʹÓÃʾÀý ===");
- var testStr = " Hello, JavaScript World! ";
- printl("»ù´¡²âÊÔ×Ö·û´®£º'" + testStr + "'£¨º¬Á½¶Ë¿Õ¸ñ£©");
- printl("---------------------------------------------\n");
- // ʾÀýµ÷ÓÃ
- printl("1. »ñÈ¡×Ö·û´®³¤¶È:");
- printl(" StrUtil.getLength('" + testStr + "') => " + StrUtil.getLength(testStr));
- printl("\n2. »ñȡָ¶¨Î»ÖÃ×Ö·û:");
- printl(" StrUtil.charAt('" + testStr + "', 7) => '" + StrUtil.charAt(testStr, 7) + "'");
- printl("\n3. ²éÕÒ×Ó×Ö·û´®Î»ÖÃ:");
- printl(" StrUtil.indexOf('" + testStr + "', 'JavaScript') => " + StrUtil.indexOf(testStr, 'JavaScript'));
- printl("\n4. Ìæ»»×Ö·û´®:");
- printl(" StrUtil.replace('" + testStr + "', 'JavaScript', 'JS') => '" + StrUtil.replace(testStr, 'JavaScript', 'JS') + "'");
- printl("\n5. È«²¿Ìæ»»:");
- printl(" StrUtil.replaceAll('" + testStr + "', 'l', 'L') => '" + StrUtil.replaceAll(testStr, 'l', 'L') + "'");
- printl("\n6. ×Ö·û´®·Ö¸î:");
- var parts = StrUtil.split(testStr, ' ');
- printl(" StrUtil.split('" + testStr + "', ' ') => " + JSON.stringify(parts));
- printl("\n7. È¥³ýÊ×β¿Õ¸ñ:");
- printl(" StrUtil.trim('" + testStr + "') => '" + StrUtil.trim(testStr) + "'");
- printl("\n8. ת»»´óСд:");
- printl(" StrUtil.toUpperCase('" + StrUtil.trim(testStr) + "') => '" + StrUtil.toUpperCase(StrUtil.trim(testStr)) + "'");
- printl(" StrUtil.toLowerCase('" + StrUtil.trim(testStr) + "') => '" + StrUtil.toLowerCase(StrUtil.trim(testStr)) + "'");
- printl("\n9. ÅжÏÊÇ·ñÒÔij×Ö·û´®¿ªÍ·:");
- printl(" StrUtil.startsWith('" + testStr + "', ' Hello') => " + StrUtil.startsWith(testStr, ' Hello'));
- printl("\n10. ×Ö·û´®Á¬½Ó:");
- printl(" StrUtil.concat('Hello', ' ', 'World', '!') => '" + StrUtil.concat('Hello', ' ', 'World', '!') + "'");
- printl("\n=== ✅ ×Ö·û´®¹¤¾ßÀà·âװʾÀýÔËÐÐÍê±Ï ===");
¸´ÖÆ´úÂë
|
|