Example of using string trim () in JS, jstrim

Source: Internet
Author: User
Tags string to number

Example of using string trim () in JS, jstrim

Example 1:

<Head> <title> test JS Extension Method </title> <script type = "text/javascript"> // Merge multiple spaces into one blank String. prototype. resetBlank = function () {// extend var regEx =/\ s +/g; return this. replace (regEx, '') ;}; window. onload = function () {var str = "are you sure you want to be okay? "; Alert (str); str = str. ResetBlank (); // you can call it. It is similar to C! Alert (str) ;}</script> 

Example 2:

// Clear the space String on both sides. prototype. trim = function () {return this. replace (/(^ \ s *) | (\ s * $)/g, '') ;}; // Merge multiple spaces 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, '') ;}; // retain the Chinese String. prototype. getCN = function () {var regEx =/[^ \ u4e00-\ u9fa5 \ uf900-\ ufa2d]/ G; return this. replace (regEx, '') ;}; // convert String to Number String. prototype. ToInt = function () {return isNaN (parseInt (this ))? This. toString (): parseInt (this) ;}; // obtain the byte length String. prototype. getLen = function () {var regEx =/^ [\ u4e00-\ u9fa5 \ uf900-\ 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 the full name of the file String. prototype. getFileName = function () {var regEx =/^. * \/([^ \/\?] *). * $/; Return this. replace (regEx, '$ 1') ;}; // get the file extension String. prototype. getExtensionName = function () {var regEx =/^. * \/[^ \/] * (\. [^ \. \?] *). * $/; Return this. replace (regEx, '$ 1') ;}; // replace all strings. 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 ($0, $1) {return args [parseInt ($1) + 1] ;}); return result ;}; // Add a Number to zero 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) ;}; // The number array is composed Small to large sort 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 ;}; // The number Array is sorted from large to small. Array. prototype. max2Min = 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 ;}; // obtain the largest Array in the number Array. prototype. getMax = function () {var oValue = 0; for (var I = 0; I <this. length; I ++) {if (this [I]> oValue) {oValue = this [I] ;}return oValue ;}; // obtain the smallest Array of numbers. prototype. getMin = function () {var oValue = 0; for (var I = 0; I <this. length; I ++) {if (this [I] <oValue) {oValue = this [I] ;}} return OValue ;}; // obtain the Chinese Date of the current time. 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. getMi Nutes (). 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 ('20140901 '). 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, // Hour "H +": this. getHours (), // Hour "m +": this. getMinutes (), // minute "s +": this. getSeconds (), // second "q +": Math. floor (this. getMonth () + 3)/3), // quarter "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. getFullY Ear () + ""). substr (4-RegExp. $1. length);} if (/(E + )/. test (format) {format = format. replace (RegExp. $1, (RegExp. $1. length> 1 )? (RegExp. $1. length> 2? "\ U661f \ u671f": "\ 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]): ("00" + o [k]). substr ("" + o [k]). length) ;}} return format;} Date. prototype. diff = function (interval, objDate) {// return undefined if (arguments. length <2 | objDate. constructor! = Date) {return undefined;} switch (interval) {// calculate the second difference case's ': return parseInt (objDate-this)/1000 ); // calculate the deviation case 'N': return parseInt (objDate-this)/60000); // calculate the time difference case 'H': return parseInt (objDate-this) /3600000); // calculate the daily variance case 'D': return parseInt (objDate-this)/86400000); // calculate the weekly variance case 'W ': return parseInt (objDate-this)/(86400000*7); // calculate the monthly difference case 'M': return (objDate. getMonth () + 1) + (objDate. getFullYear ()-this. getFullYear () * 12)-(this. getMonth () + 1); // calculate the year difference case 'y': return objDate. getFullYear ()-this. getFullYear (); // incorrect input default: return undefined ;}}; // check whether the Object is empty. 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 = '') {// null flag = true;} else {// not empty obj = obj. toUpperCase (); if (obj = 'null' | obj = 'undefined' | obj = '{}') {flag = true ;}}} else {flag = false;} return flag;

The above is all the content of this article. I hope you will like it.

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.