Example of string trim () use in JS

Source: Internet
Author: User
Tags current time getdate numeric javascript extension reserved trim

This article is mainly to introduce the JavaScript extension method string trim () The use of the guide, very simple and practical, the need for small partners can refer to.

Example one:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17-18

Example two:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231-232 233 Clear both sides of the space String.prototype.trim = function () {return this.replace (^s*) | ( s*$)/g, ""); }; Merges multiple blanks into a blank String.prototype.ResetBlank = function () {var regex =/s+/g; return this.replace (RegEx, ');  //reserved Number String.prototype.GetNum = function () {var regex =/[^d]/g return This.replace (RegEx, ');  //reserved Chinese String.prototype.GetCN = function () {var regex =/[^u4e00-u9fa5uf900-ufa2d]/g return This.replace (RegEx, ''); };  //String converted to Number String.prototype.ToInt = function () {return isNaN (parseint (this))? This.tostring (): parseint ( this); };  //Get byte length String.prototype.GetLen = function () {var regEx =/^[u4e00-u9fa5uf900-ufa2d]+$/; if (Regex.test (this)) { return this.length * 2; else {var omatches = This.match (/[x00-xff]/g); var olength = this.length * 2-omatches.length; return olength;}};  //Get file Full name String.prototype.GetFileName = function () {var regEx =/^.*/([^/?] *).*$/; Return This.replace (regEx, ' $ '); };  //Get file name extension STRING.PRototype. Getextensionname = function () {var regEx =/^.*/[^/]* (. [ ^.?] *).*$/; Return This.replace (regEx, ' $ '); };  //Replace All String.prototype.replaceAll = function (Reallydo, replacewith, ignoreCase) {if (!) RegExp.prototype.isPrototypeOf (Reallydo)) {return this.replace (new RegExp Reallydo, ignoreCase? "GI": "G")), replacewith); else {return This.replace (Reallydo, replacewith);}}; format string add by Liu Jingning 2010-12-09 String.Format = function () {if (arguments.length = = 0) {return ';}   if (arguments . length = = 1) {return arguments[0];}   var reg =/{(d+)?} /g; var args = arguments; var result = Arguments[0].replace (Reg, Function ($, $) {return Args[parseint ($) + 1];}); return result; };  /Digital complement 0 Number.prototype.LenWithZero = function (ocount) {var strText = this.tostring (); while (Strtext.length < Ocount) {StrText = ' 0 ' + strText;} return strText; };  //unicode Restore Number.prototype.ChrW = function () {return string.fromcharcode (this);  // Numeric arrays are sorted from small to large Array.prototype.Min2Max = function () {var ovalue; for (var i = 0; i < this.length; i++) {for (var j = 0; J <= I; J + +) {if (This[i] < this[j]) {ovalue = This[i]; This[i] = This[j]; This[j] = Ovalue;}} return this; };  //numeric array from large to small sort Array.prototype.Max2Min = function () {var ovalue; for (var i = 0; i < this.length; i++) {for (VA R j = 0; J <= I; J + +) {if (This[i] > This[j]) {ovalue = This[i]; This[i] = This[j]; This[j] = Ovalue;}} return this; };  //Get the maximum item Array.prototype.GetMax = function () {var ovalue = 0; for (var i = 0; i < this.length; i++) {if ( This[i] > Ovalue) {ovalue = this[i];} return ovalue; };  //Get the minimum item Array.prototype.GetMin = function () {var ovalue = 0; for (var i = 0; i < this.length; i++) {if ( This[i] < Ovalue) {ovalue = this[i];} return ovalue; };  //Get the Chinese form of the current time Date.prototype.GetCNDate = function () {var odatetext = '; Odatetext + = This.getfullyear (). Lenwithzero (4) +New Number (24180). ChrW (); Odatetext + = This.getmonth (). Lenwithzero (2) + new number (26376). ChrW (); Odatetext + = This.getdate (). Lenwithzero (2) + new number (26085). ChrW (); Odatetext + = This.gethours (). Lenwithzero (2) + new number (26102). ChrW (); Odatetext + = This.getminutes (). Lenwithzero (2) + new number (20998). ChrW (); Odatetext + = This.getseconds (). Lenwithzero (2) + new number (31186). ChrW (); Odatetext + = new Number (32). ChrW () + new number (32). ChrW () + new number (26143). ChrW () + new number (26399). ChrW () + new String (' 26085199682010819977222352011620845 '). substr (This.getday () * 5, 5). ToInt (). ChrW (); return odatetext; }; Extended Date Format Date.prototype.Format = function (format) {var o = {"m+": This.getmonth () + 1,//month "d+": this.getdate (),//day "h+": this.gethours ()% 12 = 0? 12:this.gethours ()% 12,//h "h+": this.gethours (),//Hour "m+": this.getminutes (),//min "s+": This.getseconds (),//sec "q+" : Math.floor ((This.getmonth () + 3)/3),//Quarterly "S": this.getmilliseconds ()//millisecond}; var week = {"0": "U65e5 "," 1 ":" U4e00 "," 2 ":" u4e8c "," 3 ":" U4e09 "," 4 ":" U56db "," 5 ":" U4e94 "," 6 ":" U516D "}; if (/(y+)/.test (format)) {format = Format.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length)); if (/(e+)/.test (format)) {format = Format.replace (regexp.$1, (Regexp.$1.length > 1)? (Regexp.$1.length > 2?) "u661fu671f": "u5468"): "") + week[this.getday () + ""]); For (var k in O) {if (New RegExp ("+ K +")). Test (format) {format = Format.replace (regexp.$1, regexp.$1.length = 1 ) ? (O[k]): (("+ o[k]"). substr (("" + o[k). length));} return format; Date.prototype.Diff = function (interval, objdate) {//If the parameter is insufficient or objdate is not a date type return undefined if (Arguments.length < 2 | | Objdate.constructor!= Date) {return undefined;} switch (interval) {//Calculate seconds Difference case ' s ': Return parseint ((objdate-this) /1000); Calculate the difference case ' n ': Return parseint ((objdate-this)/60000); Calculate time difference case ' h ': Return parseint ((objdate-this)/3600000); Calculate the day difference case ' d ': return parseint ((objdate-this)/86400000); Calculates the week difference case ' W ': Return parseint ((objdate-this)/(86400000 * 7)); Calculate month difference case ' m ': Return (Objdate.getmonth () + 1) + ((Objdate.getfullyear ()-this.getfullyear ())-(This.getmonth () + 1); Calculate the year difference case ' y ': return objdate.getfullyear ()-this.getfullyear (); Input error default:return undefined; } };  //detection is null Object.prototype.IsNullOrEmpty = function () {var obj = this; var flag = false; if (obj = null | | | obj = = Undefined | | typeof (obj) = = ' Undefined ' | | obj = = ') {flag = true;} else if (typeof (obj) = ' string ') {obj = Obj.trim (); if (obj = = ') {/is empty flag = true;} els e {//is not null obj = Obj.touppercase (); if (obj = = ' NULL ' | | | obj = = ' UNDEFINED ' | |-obj = = ' {} ') {flag = True;}}} else {flag = false;} return flag;

The above is the entire contents of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.