JS Built-in function
concat
Combine two or more characters of text , Returns a new string .
var a = "hello";
var b = ",world";
var c = a.concat(b);
alert(c);
//c = "hello,world"
indexOf
Returns the index of the first occurrence of a substring in a string ( Left to right search ). If there is no match , return -1 .
var index1 = a.indexOf("l");
//index1 = 2
var index2 = a.indexOf("l",3);
//index2 = 3
charAt
Returns the character in the specified position .
var get_char = a.charAt(0);
//get_char = "h"
lastIndexOf
Returns the index of the last occurrence of a substring in a string ( Right to left search ), If there is no match , return -1 .
var index1 = lastIndexOf('l');
//index1 = 3
var index2 = lastIndexOf('l',2)
//index2 = 2
match
Check that a string matches the content of a regular expression , If there is no match, return null.
var re = new RegExp(/^\w+$/);
var is_alpha1 = a.match(re);
//is_alpha1 = "hello"
var is_alpha2 = b.match(re);
//is_alpha2 = null
substring
Returns a substring of a string , The incoming parameters are the start and end positions .
var sub_string1 = a.substring(1);
//sub_string1 = "ello"
var sub_string2 = a.substring(1,4);
//sub_string2 = "ell"
substr
Returns a substring of a string , The incoming parameters are the starting position and length
var sub_string1 = a.substr(1);
//sub_string1 = "ello"
var sub_string2 = a.substr(1,4);
//sub_string2 = "ello"
replace
To find a string that matches a regular expression , Then use the new string instead of the matching string .
var result1 = a.replace(re,"Hello");
//result1 = "Hello"
var result2 = b.replace(re,"Hello");
//result2 = ",world"
search
Perform a regular expression match lookup . If the search is successful , Returns the matching index value in the string . Otherwise return to -1 .
var index1 = a.search(re);
//index1 = 0
var index2 = b.search(re);
//index2 = -1
slice
Extract part of a string , And return a new string ( And substring identical ).
var sub_string1 = a.slice(1);
//sub_string1 = "ello"
var sub_string2 = a.slice(1,4);
//sub_string2 = "ell"
split
By dividing a string into substrings , Make a string into an array of strings .
var arr1 = a.split("");
//arr1 = [h,e,l,l,o]
length
Returns the length of the string , The length of a string is the number of characters it contains .
var len = a.length();
//len = 5
toLowerCase
Convert the entire string to lowercase letters .
var lower_string = a.toLowerCase();
//lower_string = "hello"
toUpperCase
Convert the entire string to uppercase letters .
var upper_string = a.toUpperCase();
//upper_string = "HELLO"

/*
******************************************
String function extension                                 
******************************************
*/

/*
===========================================
// Remove the space on the left
===========================================

*/
String.prototype.LTrim = function()
{
return this.replace(/(^\s*)/g, "");
}

/*
===========================================
// Remove the space on the right
===========================================
*/
String.prototype.Rtrim = function()
{
return this.replace(/(\s*$)/g, "");
}

/*
===========================================
// Remove the space before and after
===========================================
*/
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}

/*
===========================================
// Get the string on the left
===========================================
*/
String.prototype.Left = function(len)
{

if(isNaN(len)||len==null)
{
len = this.length;
}
else
{
if(parseInt(len)<0||parseInt(len)>this.length)
{
len = this.length;
}
}

return this.substr(0,len);
}

/*
===========================================
// Get the string on the right
===========================================
*/
String.prototype.Right = function(len)
{

if(isNaN(len)||len==null)
{
len = this.length;
}
else
{
if(parseInt(len)<0||parseInt(len)>this.length)
{
len = this.length;
}
}

return this.substring(this.length-len,this.length);
}

/*
===========================================
// Get the middle string , Attention from 0 Start
===========================================
*/
String.prototype.Mid = function(start,len)
{
return this.substr(start,len);
}

/*
===========================================
// Find another string in a string : Location slave 0 Start
===========================================
*/
String.prototype.InStr = function(str)
{

if(str==null)
{
str = "";
}

return this.indexOf(str);
}

/*
===========================================
// Looking up another string in a string in reverse : Location 0 Start
===========================================
*/
String.prototype.InStrRev = function(str)
{

if(str==null)
{
str = "";
}

return this.lastIndexOf(str);
}

/*
===========================================
// Calculate string print length
===========================================
*/
String.prototype.LengthW = function()
{
return this.replace(/[^\x00-\xff]/g,"**").length;
}

/*
===========================================
// Is it right IP Address
===========================================
*/
String.prototype.isIP = function()
{

var reSpaceCheck = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;

if (reSpaceCheck.test(this))
{
this.match(reSpaceCheck);
if (RegExp.$1 <= 255 && RegExp.$1 >= 0
&& RegExp.$2 <= 255 && RegExp.$2 >= 0
&& RegExp.$3 <= 255 && RegExp.$3 >= 0
&& RegExp.$4 <= 255 && RegExp.$4 >= 0)
{
return true;    
}
else
{
return false;
}
}
else
{
return false;
}

}

/*
===========================================
// Is it the right long date
===========================================
*/
String.prototype.isLongDate = function()
{
var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
if(r==null)
{
return false;
}
var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);

}

/*
===========================================
// Is it the right short date
===========================================
*/
String.prototype.isShortDate = function()
{
var r = this.replace(/(^\s*)|(\s*$)/g, "").match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)
{
return false;
}
var d = new Date(r[1], r[3]-1, r[4]);
return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
}

/*
===========================================
// Is it the right date
===========================================
*/
String.prototype.isDate = function()
{
return this.isLongDate()||this.isShortDate();
}

/*
===========================================
// Whether it's a cell phone
===========================================
*/
String.prototype.isMobile = function()
{
return /^0{0,1}13[0-9]{9}$/.test(this);
}

/*
===========================================
// Whether it's mail
===========================================
*/
String.prototype.isEmail = function()
{
return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);
}

/*
===========================================
// Is it a postcode ( China )
===========================================
*/

String.prototype.isZipCode = function()
{
return /^[\\d]{6}$/.test(this);
}

/*
===========================================
// Whether there are Chinese characters
===========================================
*/
String.prototype.existChinese = function()
{
//[\u4E00-\u9FA5] For Chinese characters ﹐[\uFE30-\uFFA0] It's a full angle symbol
return /^[\x00-\xff]*$/.test(this);
}

/*
===========================================
// Is it a legal file name / Directory name
===========================================
*/
String.prototype.isFileName = function()
{
return !/[\\\/\*\?\|:"<>]/g.test(this);
}

/*
===========================================
// Whether it is a valid link
===========================================
*/
String.prototype.isUrl = function()
{
return /^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/i.test(this);
}

/*
===========================================
// Is it a valid ID card ( China )
===========================================
*/
String.prototype.isIDCard = function()
{
var iSum=0;
var info="";
var sId = this;

 var aCity={11:" Beijing ",12:" tianjin ",13:" hebei ",14:" shanxi ",15:" Inner Mongolia ",21:" liaoning ",22:" Ji Lin ",23:" Black Dragon jiang ",31:" Shanghai ",32:" jiangsu ",33:" Zhejiang ",34:" anhui ",35:" fujian ",36:" jiangxi ",37:" Shandong ",41:" Henan ",42:" lake north ",43:" hunan ",44:" guangdong ",45:" guangxi ",46:" hainan ",50:" Chongqing ",51:" sichuan ",52:" guizhou ",53:" yunnan ",54:" Tibet ",61:" shaanxi ",62:" gansu ",63:" qinghai ",64:" ningxia ",65:" xinjiang ",71:" Taiwan ",81:" Hong Kong ",82:" Macau ",91:" Abroad "};

if(!/^\d{17}(\d|x)$/i.test(sId))
{
return false;
}
sId=sId.replace(/x$/i,"a");
// Illegal areas
if(aCity[parseInt(sId.substr(0,2))]==null)
{
return false;
}

var sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));

var d=new Date(sBirthday.replace(/-/g,"/"))

// Illegal birthday
if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
{
return false;
}
for(var i = 17;i>=0;i--)
{
iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11);
}

if(iSum%11!=1)
{
return false;
}
return true;

}

/*
===========================================
// Is a valid phone number ( China )
===========================================
*/
String.prototype.isPhoneCall = function()
{
return /(^[0-9]{3,4}\-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^\([0-9]{3,4}\)[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this);
}

/*
===========================================
// Is it a number
===========================================
*/
String.prototype.isNumeric = function(flag)
{
// Verify that it's a number
if(isNaN(this))
{

return false;
}

switch(flag)
{

case null:        // Numbers
case "":
return true;
case "+":        // Positive numbers
return                /(^\+?|^\d?)\d*\.?\d+$/.test(this);
case "-":        // negative
return                /^-\d*\.?\d+$/.test(this);
case "i":        // Integers
return                /(^-?|^\+?|\d)\d+$/.test(this);
case "+i":        // Positive integer
return                /(^\d+$)|(^\+?\d+$)/.test(this);                       
case "-i":        // Negtive integer
return                /^[-]\d+$/.test(this);
case "f":        // Floating point numbers
return                /(^-?|^\+?|^\d?)\d*\.\d+$/.test(this);
case "+f":        // Positive floating point
return                /(^\+?|^\d?)\d*\.\d+$/.test(this);                       
case "-f":        // Negative floating point number
return                /^[-]\d*\.\d$/.test(this);               
default:        // default
return true;                       
}
}

/*
===========================================
// Whether it's color (#FFFFFF form )
===========================================
*/
String.prototype.IsColor = function()
{
var temp        = this;
if (temp=="") return true;
if (temp.length!=7) return false;
return (temp.search(/\#[a-fA-F0-9]{6}/) != -1);
}

/*
===========================================
// Convert to full angle
===========================================
*/
String.prototype.toCase = function()
{
var tmp = "";
for(var i=0;i<this.length;i++)
{
if(this.charCodeAt(i)>0&&this.charCodeAt(i)<255)
{
tmp += String.fromCharCode(this.charCodeAt(i)+65248);
}
else
{
tmp += String.fromCharCode(this.charCodeAt(i));
}
}
return tmp
}

/*
===========================================
// On the string Html code
===========================================
*/
String.prototype.toHtmlEncode = function()
{
var str = this;

str=str.replace(/&/g,"&amp;");
str=str.replace(/</g,"&lt;");
str=str.replace(/>/g,"&gt;");
str=str.replace(/\'/g,"&apos;");
str=str.replace(/\"/g,"&quot;");
str=str.replace(/\n/g,"<br>");
str=str.replace(/\ /g,"&nbsp;");
str=str.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");

return str;
}

/*
===========================================
// Convert to date
===========================================
*/
String.prototype.toDate = function()
{
try
{
return new Date(this.replace(/-/g, "\/"));
}
catch(e)
{
return null;
}
}

【repost】js String function more related articles

  1. js String function

    JS Built-in function concat Combine two or more characters of text , Returns a new string .var a = "hello";var b = ",world";var c = a ...

  2. js String function [http://www.cnblogs.com/qfb620/archive/2011/07/28/2119799.html]

    JS Built-in function concat Combine two or more characters of text , Returns a new string .var a = "hello";var b = ",world";var c = a ...

  3. js String function (split、join、indexOf、substring)

    1, function :split() function : Use a specified separator to split a string and store it in the array sample : str="jpg|bmp|gif|ico|png";arr= str .split(" ...

  4. js String function substring() substr()

    substring Method is used to extract characters that are mediated between two specified subscripts substring(start,end) Where to start and where to end , Index from scratch Parameters     describe start     It's necessary . A nonnegative integer ...

  5. js String function split()join()

    split Method is used to cut a string into an array of strings , And join contrary A parameter indicates that it is the cutting point , var str="silence's world"; console.log(str ...

  6. js String function indexOf()

    indexOf Returns the first occurrence of a specified character in a string var str="hello, I am Miss bean!"; str.indexOf("l")// result ...

  7. 2018-06-20 js String function

    str.length -> String length ; str.indexOf() -> Find the position of a character in the string from the left : str.lastIndexOf -> Find the position of a character in the string from the right : ...

  8. js perform js String function method

    <script> var jsText = 'return function(){alert(1+1)}' var jscode = new Function(jsText)(); jsc ...

  9. js String manipulation functions

    js String function JS Built-in function concat Combine two or more characters of text , Returns a new string . var a = "hello"; var b = ",world&quo ...

Random recommendation

  1. memcache Memory recovery mechanism

    memcache It doesn't free memory , It's about reusing . In terms of cache cleanup ,memcache Is not releasing allocated memory . When the record in which the allocated memory is located fails , This past memory space ,memcache Only reuse . memcache ...

  2. Use supervisor Some do's and don 'ts

    It's been used all the time supervisor To manage linux Service process on . Recently, a colleague said that there seems to be something wrong with a certain service , Let's go up and check . After going up, I found that the response of a service was really slow , So just use it. supervisor Restart it . But when it restarts, it sends ...

  3. PDFium-PDF Open source journey (1)- Download and compile the code

    In recent days, ,Google and Foxit Cooperation is open source Foxit Of PDF Source code . It's called PDFium There's a lot of news . Ha , It's not a programming ape anymore . I'm just interested in running and watching . Don't talk nonsense . Let's start with the steps of compiling code ( in fact Googl ...

  4. Decorator mode of design mode introduction Decorator

    // Decoration pattern definition : Dynamically add additional responsibilities to an object . // In terms of adding features . Decoration patterns are more flexible than subclasses // This also presents a basic principle in object-oriented design , namely : Try to use object combinations , Instead of object inheritance //Compo ...

  5. JAVA The use of basic library

    Idea Middle double click SHIFT You can search [Eclipse There are also ], This is very important , You can find the existing classes in the current project that can be used as engine , Don't invent the wheel yourself again Various Util First look in the basic library and open source library Base64 code guava:c ...

  6. [2017-7-28]Android Learning Day7

    View Animation effect Transparent animation effects Rotating animation effects Mobile animation effects Zoom animation Mixed animation effects 1. Transparent animation effects (AlphaAnimation) There are two ways The first is set in the activity , Unwanted xml file public ...

  7. Gulp Error: Cannot find module &#39;jshint/src/cli&#39;

    I'm following sitepoint's An introduction to Gulp.js, but I'm stuck on step four, when I try to run  ...

  8. HDU 2021 Pay wages :)

    http://acm.hdu.edu.cn/showproblem.php?pid=2021 Problem Description As a teacher of Hangdian , The most expected day is every month 8 The no. , Because this day is paid ...

  9. [No000017E] improve C# Suggestions for procedures 7: Stop the thread correctly

    Developers always try to have more control over their code .“ Let the working thread stop immediately ” Is one of many requirements . But it backfired , There are at least two problems : The first question is : Just as a thread cannot start immediately , Threads can't stop just by stopping . nothing ...

  10. win7 Notepad txt Icon shows exception resolution

    link :https://zhidao.baidu.com/question/2076205353435701108.html                                       ...