B2BÍøÂçÈí¼þ

 ÕÒ»ØÃÜÂë
 Á¢¼´×¢²á ÉóºËÍøÕ¾ºÅ:QQ:896757558
ËÑË÷
²é¿´: 8|»Ø¸´: 0
´òÓ¡ ÉÏÒ»Ö÷Ìâ ÏÂÒ»Ö÷Ìâ

AIWROKÈí¼þStringʵÀýÑÝʾ

[¸´ÖÆÁ´½Ó]

983

Ö÷Ìâ

988

Ìû×Ó

7209

»ý·Ö

abc

Rank: 9Rank: 9Rank: 9

»ý·Ö
7209
Ìø×ªµ½Ö¸¶¨Â¥²ã
Â¥Ö÷
·¢±íÓÚ 9 Ð¡Ê±Ç° | Ö»¿´¸Ã×÷Õß »ØÌû½±Àø |µ¹Ðòä¯ÀÀ |ÔĶÁģʽ
AIWROKÈí¼þStringʵÀýÑÝʾ

  1. /*
  2. 🍎½»Á÷QQȺ711841924Ⱥһ£¬Æ»¹ûÄÚ²âȺ£¬528816639
  3. 🍎🔨JavaScript×Ö·û´®¹¤¾ßÀà·â×°
  4. 🔨ÊÊÓñ¾ÎĵµES5ϵͳ°²×¿ JavaScriptÒýÇæRhino
  5. */

  6. /**
  7. * ×Ö·û´®¹¤¾ßÀà·â×°
  8. * Ìṩ¸ü¼ÓÃæÏò¶ÔÏóµÄ×Ö·û´®²Ù×÷½Ó¿Ú
  9. */
  10. function StringUtils() {}

  11. /**
  12. * ·µ»ØÖ¸¶¨Î»ÖõÄ×Ö·û
  13. * @param {string} str - Ô´×Ö·û´®
  14. * @param {number} index - ×Ö·ûλÖÃ
  15. * @returns {string} Ö¸¶¨Î»ÖõÄ×Ö·û
  16. */
  17. StringUtils.prototype.charAt = function(str, index) {
  18.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  19.     if (typeof index !== 'number' || index % 1 !== 0) throw new Error('[´íÎó] ²ÎÊýindex±ØÐëÊÇÕûÊý');
  20.     return str.charAt(index);
  21. };

  22. /**
  23. * ·µ»ØÖ¸¶¨Î»ÖÃ×Ö·ûµÄUnicode±àÂë
  24. * @param {string} str - Ô´×Ö·û´®
  25. * @param {number} index - ×Ö·ûλÖÃ
  26. * @returns {number} Unicode±àÂëÖµ
  27. */
  28. StringUtils.prototype.charCodeAt = function(str, index) {
  29.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  30.     if (typeof index !== 'number' || index % 1 !== 0) throw new Error('[´íÎó] ²ÎÊýindex±ØÐëÊÇÕûÊý');
  31.     return str.charCodeAt(index);
  32. };

  33. /**
  34. * ²éÕÒ×Ó×Ö·û´®Ê״γöÏÖµÄλÖÃ
  35. * @param {string} str - Ô´×Ö·û´®
  36. * @param {string} searchValue - Òª²éÕÒµÄ×Ó×Ö·û´®
  37. * @param {number} [fromIndex=0] - ¿ªÊ¼²éÕÒµÄλÖÃ
  38. * @returns {number} Ê״γöÏÖµÄλÖã¬Î´ÕÒµ½·µ»Ø-1
  39. */
  40. StringUtils.prototype.indexOf = function(str, searchValue, fromIndex) {
  41.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  42.     if (typeof searchValue !== 'string') throw new Error('[´íÎó] ²ÎÊýsearchValue±ØÐëÊÇ×Ö·û´®');
  43.     return str.indexOf(searchValue, fromIndex);
  44. };

  45. /**
  46. * ²éÕÒ×Ó×Ö·û´®×îºóÒ»´Î³öÏÖµÄλÖÃ
  47. * @param {string} str - Ô´×Ö·û´®
  48. * @param {string} searchValue - Òª²éÕÒµÄ×Ó×Ö·û´®
  49. * @param {number} [fromIndex=str.length] - ¿ªÊ¼²éÕÒµÄλÖÃ
  50. * @returns {number} ×îºóÒ»´Î³öÏÖµÄλÖã¬Î´ÕÒµ½·µ»Ø-1
  51. */
  52. StringUtils.prototype.lastIndexOf = function(str, searchValue, fromIndex) {
  53.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  54.     if (typeof searchValue !== 'string') throw new Error('[´íÎó] ²ÎÊýsearchValue±ØÐëÊÇ×Ö·û´®');
  55.     return str.lastIndexOf(searchValue, fromIndex);
  56. };

  57. /**
  58. * »ñÈ¡×Ö·û´®³¤¶È
  59. * @param {string} str - Ô´×Ö·û´®
  60. * @returns {number} ×Ö·û´®³¤¶È
  61. */
  62. StringUtils.prototype.getLength = function(str) {
  63.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  64.     return str.length;
  65. };

  66. /**
  67. * ʹÓÃÕýÔò±í´ïʽƥÅä×Ö·û´®
  68. * @param {string} str - Ô´×Ö·û´®
  69. * @param {RegExp} regexp - ÕýÔò±í´ïʽ
  70. * @returns {Array|null} Æ¥Åä½á¹ûÊý×飬δƥÅä·µ»Ønull
  71. */
  72. StringUtils.prototype.match = function(str, regexp) {
  73.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  74.     if (!(regexp instanceof RegExp)) throw new Error('[´íÎó] ²ÎÊýregexp±ØÐëÊÇÕýÔò±í´ïʽ¶ÔÏó');
  75.     return str.match(regexp);
  76. };

  77. /**
  78. * Ìæ»»µÚÒ»¸öÆ¥ÅäµÄ×Ó×Ö·û´®
  79. * @param {string} str - Ô´×Ö·û´®
  80. * @param {string|RegExp} searchValue - ÒªÌæ»»µÄÄÚÈÝ
  81. * @param {string|Function} replaceValue - Ìæ»»ÄÚÈÝ
  82. * @returns {string} Ìæ»»ºóµÄÐÂ×Ö·û´®
  83. */
  84. StringUtils.prototype.replace = function(str, searchValue, replaceValue) {
  85.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  86.     return str.replace(searchValue, replaceValue);
  87. };

  88. /**
  89. * Ìæ»»ËùÓÐÆ¥ÅäµÄ×Ó×Ö·û´®
  90. * @param {string} str - Ô´×Ö·û´®
  91. * @param {string|RegExp} searchValue - ÒªÌæ»»µÄÄÚÈÝ
  92. * @param {string|Function} replaceValue - Ìæ»»ÄÚÈÝ
  93. * @returns {string} Ìæ»»ºóµÄÐÂ×Ö·û´®
  94. */
  95. StringUtils.prototype.replaceAll = function(str, searchValue, replaceValue) {
  96.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  97.    
  98.     // ¼æÈݵͰ汾»·¾³£ºÈô²»Ö§³ÖreplaceAll£¬ÓÃÕýÔò/gÌæ´ú
  99.     if (!String.prototype.replaceAll) {
  100.         if (typeof searchValue === 'string') {
  101.             // ×Ö·û´®×ªÕýÔò£¨×ªÒåÌØÊâ×Ö·û£©
  102.             var escaped = searchValue.replace(/[.*+?^${}()|[\]\\]/g, '\\[        DISCUZ_CODE_0        ]amp;');
  103.             return str.replace(new RegExp(escaped, 'g'), replaceValue);
  104.         } else if (searchValue instanceof RegExp && !searchValue.global) {
  105.             // ÕýÔòÎÞ/gÐÞÊηû£¬Ìí¼ÓºóÆ¥ÅäËùÓÐ
  106.             var flags = 'g';
  107.             if (searchValue.ignoreCase) flags += 'i';
  108.             if (searchValue.multiline) flags += 'm';
  109.             var newRegexp = new RegExp(searchValue.source, flags);
  110.             return str.replace(newRegexp, replaceValue);
  111.         }
  112.     }
  113.     return str.replaceAll(searchValue, replaceValue);
  114. };

  115. /**
  116. * ·Ö¸î×Ö·û´®ÎªÊý×é
  117. * @param {string} str - Ô´×Ö·û´®
  118. * @param {string|RegExp} separator - ·Ö¸ô·û
  119. * @param {number} [limit] - ÏÞÖÆ·µ»ØÊý×é´óС
  120. * @returns {Array} ·Ö¸îºóµÄ×Ö·û´®Êý×é
  121. */
  122. StringUtils.prototype.split = function(str, separator, limit) {
  123.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  124.     return str.split(separator, limit);
  125. };

  126. /**
  127. * ÅжÏ×Ö·û´®ÊÇ·ñÒÔÖ¸¶¨ÄÚÈÝ¿ªÍ·
  128. * @param {string} str - Ô´×Ö·û´®
  129. * @param {string} searchValue - Òª¼ì²âµÄÄÚÈÝ
  130. * @param {number} [position=0] - ¼ì²â¿ªÊ¼Î»ÖÃ
  131. * @returns {boolean} ÊÇ·ñÒÔÖ¸¶¨ÄÚÈÝ¿ªÍ·
  132. */
  133. StringUtils.prototype.startsWith = function(str, searchValue, position) {
  134.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  135.     if (typeof searchValue !== 'string') throw new Error('[´íÎó] ²ÎÊýsearchValue±ØÐëÊÇ×Ö·û´®');
  136.    
  137.     // ¼æÈݵͰ汾»·¾³£ºÊÖ¶¯ÊµÏÖstartsWith
  138.     if (!String.prototype.startsWith) {
  139.         var pos = position || 0;
  140.         return str.substr(pos, searchValue.length) === searchValue;
  141.     }
  142.     return str.startsWith(searchValue, position);
  143. };

  144. /**
  145. * ´ÓÖ¸¶¨Î»ÖýØÈ¡Ö¸¶¨³¤¶ÈµÄ×Ó×Ö·û´®
  146. * @param {string} str - Ô´×Ö·û´®
  147. * @param {number} start - ¿ªÊ¼Î»ÖÃ
  148. * @param {number} [length] - ½ØÈ¡³¤¶È
  149. * @returns {string} ½ØÈ¡µÄ×Ó×Ö·û´®
  150. */
  151. StringUtils.prototype.substr = function(str, start, length) {
  152.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  153.     if (typeof start !== 'number' || start % 1 !== 0) throw new Error('[´íÎó] ²ÎÊýstart±ØÐëÊÇÕûÊý');
  154.     return str.substr(start, length);
  155. };

  156. /**
  157. * ½ØÈ¡Ö¸¶¨·¶Î§µÄ×Ó×Ö·û´®
  158. * @param {string} str - Ô´×Ö·û´®
  159. * @param {number} start - ¿ªÊ¼Î»ÖÃ
  160. * @param {number} [end] - ½áÊøÎ»Ö㨲»°üº¬£©
  161. * @returns {string} ½ØÈ¡µÄ×Ó×Ö·û´®
  162. */
  163. StringUtils.prototype.substring = function(str, start, end) {
  164.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  165.     if (typeof start !== 'number' || start % 1 !== 0) throw new Error('[´íÎó] ²ÎÊýstart±ØÐëÊÇÕûÊý');
  166.     return str.substring(start, end);
  167. };

  168. /**
  169. * È¥³ý×Ö·û´®Ê×β¿Õ°××Ö·û
  170. * @param {string} str - Ô´×Ö·û´®
  171. * @returns {string} È¥³ýÊ×β¿Õ°××Ö·ûºóµÄ×Ö·û´®
  172. */
  173. StringUtils.prototype.trim = function(str) {
  174.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  175.    
  176.     // ¼æÈݵͰ汾»·¾³£ºÊÖ¶¯ÊµÏÖtrim
  177.     if (!String.prototype.trim) {
  178.         return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  179.     }
  180.     return str.trim();
  181. };

  182. /**
  183. * ת»»×Ö·û´®Îª´óд
  184. * @param {string} str - Ô´×Ö·û´®
  185. * @returns {string} ת»»ºóµÄ´óд×Ö·û´®
  186. */
  187. StringUtils.prototype.toUpperCase = function(str) {
  188.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  189.     return str.toUpperCase();
  190. };

  191. /**
  192. * ת»»×Ö·û´®ÎªÐ¡Ð´
  193. * @param {string} str - Ô´×Ö·û´®
  194. * @returns {string} ת»»ºóµÄСд×Ö·û´®
  195. */
  196. StringUtils.prototype.toLowerCase = function(str) {
  197.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  198.     return str.toLowerCase();
  199. };

  200. /**
  201. * Á¬½Ó×Ö·û´®
  202. * @param {string} str - Ô´×Ö·û´®
  203. * @param {...string} strings - ÒªÁ¬½ÓµÄ×Ö·û´®
  204. * @returns {string} Á¬½ÓºóµÄ×Ö·û´®
  205. */
  206. StringUtils.prototype.concat = function(str) {
  207.     if (typeof str !== 'string') throw new Error('[´íÎó] ²ÎÊýstr±ØÐëÊÇ×Ö·û´®');
  208.     var args = Array.prototype.slice.call(arguments, 1);
  209.     return str.concat.apply(str, args);
  210. };

  211. // ´´½¨È«¾ÖʵÀý£¬·½±ãÖ±½Óµ÷ÓÃ
  212. var StrUtil = new StringUtils();

  213. // ===============================
  214. // ʹÓÃʾÀý
  215. // ===============================
  216. function printl() {
  217.     // ÊÊÅä´òÓ¡º¯Êý£º¶¨ÒåprintlÓÃÓÚÊä³ö
  218.     var args = Array.prototype.slice.call(arguments);
  219.    
  220.     // Èô»·¾³Ö§³Öconsole.logÖ±½Óµ÷Óã¬Èô²»Ö§³Ö¿ÉÌæ»»ÎªÆäËûÊä³ö·½Ê½
  221.     if (typeof console !== 'undefined' && console.log) {
  222.         console.log.apply(console, args);
  223.     } else {
  224.         // ½µ¼¶´¦Àí£ºÆ´½Ó×Ö·û´®Êä³ö£¨ÊÊÅäÎÞconsole»·¾³£©
  225.         var output = args.join(' ');
  226.         if (typeof document !== 'undefined') {
  227.             document.write(output + '<br>');
  228.         } else if (typeof process !== 'undefined' && process.stdout) {
  229.             // Node.js»òÆäËû»·¾³¶µµ×
  230.             process.stdout.write(output + '\n');
  231.         }
  232.     }
  233. }

  234. printl("=== &#128640; ×Ö·û´®¹¤¾ßÀà·â×° - ʹÓÃʾÀý ===");
  235. var testStr = "  Hello, JavaScript World!  ";
  236. printl("»ù´¡²âÊÔ×Ö·û´®£º'" + testStr + "'£¨º¬Á½¶Ë¿Õ¸ñ£©");
  237. printl("---------------------------------------------\n");

  238. // ʾÀýµ÷ÓÃ
  239. printl("1. »ñÈ¡×Ö·û´®³¤¶È:");
  240. printl("   StrUtil.getLength('" + testStr + "') => " + StrUtil.getLength(testStr));

  241. printl("\n2. »ñȡָ¶¨Î»ÖÃ×Ö·û:");
  242. printl("   StrUtil.charAt('" + testStr + "', 7) => '" + StrUtil.charAt(testStr, 7) + "'");

  243. printl("\n3. ²éÕÒ×Ó×Ö·û´®Î»ÖÃ:");
  244. printl("   StrUtil.indexOf('" + testStr + "', 'JavaScript') => " + StrUtil.indexOf(testStr, 'JavaScript'));

  245. printl("\n4. Ìæ»»×Ö·û´®:");
  246. printl("   StrUtil.replace('" + testStr + "', 'JavaScript', 'JS') => '" + StrUtil.replace(testStr, 'JavaScript', 'JS') + "'");

  247. printl("\n5. È«²¿Ìæ»»:");
  248. printl("   StrUtil.replaceAll('" + testStr + "', 'l', 'L') => '" + StrUtil.replaceAll(testStr, 'l', 'L') + "'");

  249. printl("\n6. ×Ö·û´®·Ö¸î:");
  250. var parts = StrUtil.split(testStr, ' ');
  251. printl("   StrUtil.split('" + testStr + "', ' ') => " + JSON.stringify(parts));

  252. printl("\n7. È¥³ýÊ×β¿Õ¸ñ:");
  253. printl("   StrUtil.trim('" + testStr + "') => '" + StrUtil.trim(testStr) + "'");

  254. printl("\n8. ת»»´óСд:");
  255. printl("   StrUtil.toUpperCase('" + StrUtil.trim(testStr) + "') => '" + StrUtil.toUpperCase(StrUtil.trim(testStr)) + "'");
  256. printl("   StrUtil.toLowerCase('" + StrUtil.trim(testStr) + "') => '" + StrUtil.toLowerCase(StrUtil.trim(testStr)) + "'");

  257. printl("\n9. ÅжÏÊÇ·ñÒÔij×Ö·û´®¿ªÍ·:");
  258. printl("   StrUtil.startsWith('" + testStr + "', '  Hello') => " + StrUtil.startsWith(testStr, '  Hello'));

  259. printl("\n10. ×Ö·û´®Á¬½Ó:");
  260. printl("   StrUtil.concat('Hello', ' ', 'World', '!') => '" + StrUtil.concat('Hello', ' ', 'World', '!') + "'");

  261. printl("\n=== ✅ ×Ö·û´®¹¤¾ßÀà·âװʾÀýÔËÐÐÍê±Ï ===");
¸´ÖÆ´úÂë


»Ø¸´

ʹÓõÀ¾ß ¾Ù±¨

±¾°æ»ý·Ö¹æÔò

¹Ø±Õ

QQ|»ÓªÏúÈí¼þ×ÛºÏÌÖÂÛ|»ÓªÏúÈí¼þÓÐÎʱشð|»ÓªÏúÈí¼þ½Ì³Ì×¨Çø|»ÓªÏúÈí¼þPOST½Å±¾·ÖÏí|»ÓªÏúÈí¼þÆÕͨ½Å±¾·ÖÏí|»ÓªÏúÈí¼þÈí¼þ×ÊѶ|»ÓªÏúÈí¼þ¾«Æ·Èí¼þ|»ÓªÏúÈí¼þ¸üй«¸æ|ÓªÏúÈí¼þ|B2BÈí¼þ|B2BÍøÂçÈí¼þ ( ¾©ICP±¸09078825ºÅ )±¾ÍøÕ¾¿ª·¢µÄÓªÏúÈí¼þÊÇÒ»¿îеÄÍøÂçÓªÏúÈí¼þ£¬Õâ¿îÓªÏú¿ÉÒÔÈ¥ÍøÕ¾Èí¼þ£¬²©¿ÍÈí¼þ£¬B2BÈí¼þ£¬·ÖÀàÐÅÏ¢Íø·¢Ìù£¬¿ÉÒÔÇÀɳ·¢£¬¿ÉÒÔµ½°Ù¶ÈÎÄ¿âÉÏ´«WORDÎĵµ£¬¿ÉÒÔµ½Ò»Ð©ÊÇÏà²áÍøÕ¾×Ô¶¯ÉÏ´«Í¼Æ¬£¬Õâ¸ö×Ô¶¯·¢ÌûÈí¼þ×Ô´øÔÆÖ©Ö룬¼Ó¿ìÊÕ¼£¬ÓÐ6ÖÖ¶Ô½Ó´òÂë½Ó¿Ú£¬·½±ã£¬Ð§Âʸߣ¬Ëٶȿ죬¶øÇÒ¶ÔÍ϶¯µÄÑéÖ¤ÂëÈ«ÍøµÚÒ»¼Ò¶À¼ÒÖ§³Ö£¬È«²¿Ô­´´¼¼Êõ£¬¶À¼ÒÑз¢£¬Õý°æÔ­´´´ø°æÈ¨Èí¼þ¡£Ñ¡ÔñÍòÄÜÓªÏúÈí¼þ£¬¾ÍÑ¡ÔñÁËÒ»ÖÖ׬ǮµÄЧÂÊ£¬´ÓûÓб»³¬Ô½¹ý£¬Ò»Ö±ÔÚŬÁ¦Ñз¢Ð¼¼Êõ¡£·Å·ÉÃÎÏ룬½â·ÅË«ÊÖ£¬À´µã´´Ò⣬³É¾ÍÄãµÄÃÎÏ룬¾ÍÔÚÍòÄÜÓªÏúÈí¼þ¿ªÊ¼

map2

GMT+8, 2025-11-18 18:00 , Processed in 0.407843 second(s), 30 queries .

¿ìËٻظ´ ·µ»Ø¶¥²¿ ·µ»ØÁбí