One 、String Object properties

1、length attribute :

length It's a very common property in string , Its function is to get the length of the string . Of course, it's important to note that js In Chinese, each character represents only one character , It may be different from other languages .

var str = 'abc';
console.log(str.length); //

2、prototype attribute :

prototype It is often used in object-oriented programming , Used to add properties or methods to an object , And the added method or property is shared on all instances . So it's also used to extend js Built-in objects , For example, the following code adds a method to remove the spaces on both sides of the string .

String.prototype.trim = function(){
return this.replace(/^\s*|\s*$/g, '');
}

Two 、String Object methods

1、 Get class method :

(1) charAt():
stringObject.charAt(index)

charAt() Method can be used to get a string at a specified location ,index Is the string index value , from 0 Start to string.leng – 1, If not, an empty string will be returned . Such as :

var str = 'abcde';
console.log(str.charAt(2)); // return c
console.log(str.charAt(8)); // Returns an empty string
(2) charCodeAt():
stringObject.charCodeAt(index)

charCodeAt() Method to return the Unicode code .charCodeAt() Methods and charAt() The method is similar to , You need to pass in an index value as a parameter , The difference is that the former returns the encoding of the character in the specified position , The latter returns a string of characters .

var str = 'abcde';
console.log(str.charCodeAt(0)); // return 97 
(3) fromCharCode():
String.fromCharCode(numX,numX,…,numX) 

fromCharCode() One or more of Unicode value , Then return a string . The other way is String Static method of , Each character in a string is represented by a separate number Unicode Code designation .

String.fromCharCode(97, 98, 99, 100, 101); // return abcde

2. Find class methods

(1) indexOf():
stringObject.indexOf(searchvalue,fromindex)

indexOf() Used to retrieve the first occurrence of a specified string value in a string . It can take two parameters ,searchvalue Represents the substring to look up ,fromindex Indicates where the search starts , If omitted, search from the beginning .

var str = 'abcdeabcde';
console.log(str.indexOf('a')); // return 0
console.log(str.indexOf('a', 3)); // return 5
console.log(str.indexOf('bc')); // return 1
(2) lastIndexOf() Method :
stringObject.lastIndexOf(searchvalue,fromindex)

lastIndexOf() Syntax and indexOf() similar , It returns the last occurrence of a specified substring value , The retrieval order is from the back to the front .

var str = 'abcdeabcde';
console.log(str.lastIndexOf('a')); // return 5
console.log(str.lastIndexOf('a', 3)); // return 0 From the second index 3 Search ahead for the location of
console.log(str.lastIndexOf('bc')); // return 6
(3) search() Method :
stringObject.search(substr)
stringObject.search(regexp)

search() Method to retrieve the substring specified in the string , Or retrieve the substring that matches the regular expression . It returns the starting position of the first matching substring , If there is no match , Then return to -1.

var str = 'abcDEF';
console.log(str.search('c')); // return 2
console.log(str.search('d')); // return -1
console.log(str.search(/d/i)); // return 3
(4) match() Method :
stringObject.match(substr)
stringObject.match(regexp)

match() Method to retrieve the specified value in a string , Or find a match for one or more regular expressions .

If the parameter is passed in a substring or a regular expression without a global match , that match() Method will perform a match from the start , If it doesn't match the result , Then return to null. Otherwise, an array will be returned , The... Of the array 0 The elements hold the matching text , besides , The returned array also contains two object properties index and input, Indicates the starting character index of the matched text and stringObject References to ( The original string ).

var str = '1a2b3c4d5e';
console.log(str.match('h')); // return null
console.log(str.match('b')); // return ["b", index: 3, input: "1a2b3c4d5e"]
console.log(str.match(/b/)); // return ["b", index: 3, input: "1a2b3c4d5e"]

If a parameter is passed in a regular expression with a global match , that match() Match multiple times from start , Until the last . If it doesn't match the result , Then return to null. Otherwise, an array will be returned , The array holds all the required substrings , And there's no index and input attribute .

var str = '1a2b3c4d5e';
console.log(str.match(/h/g)); // return null
console.log(str.match(/\d/g)); // return ["1", "2", "3", "4", "5"]

3. Intercepting class methods

(1) substring():
stringObject.substring(start,end) 

substring() Is the most commonly used string interception method , It can take two parameters ( Parameter cannot be negative ), They are the start position and the end position to intercept , It will return a new string , Its content is from start Go to end-1 All characters at . If the end parameter (end) Omit , Says from the start The position is intercepted until the end .

var str = 'abcdefg';
console.log(str.substring(1, 4)); // return bcd
console.log(str.substring(1)); // return bcdefg
console.log(str.substring(-1)); // return abcdefg, Negative values are passed in as 0
(2) slice():
stringObject.slice(start,end) 

slice() Methods and substring() The method is very similar , The two parameters it passes in also correspond to the start position and the end position respectively . And the difference is ,slice() Parameters in can be negative values , If the parameter is negative , This parameter specifies the position from the end of the string . in other words ,-1 The last character of a string .

var str = 'abcdefg';
console.log(str.slice(1, 4)); // return bcd
console.log(str.slice(-3, -1)); // return ef
console.log(str.slice(1, -1)); // return bcdef
console.log(str.slice(-1, -3)); // Returns an empty string , If there is a problem with the parameter passed in , It returns null
(3) substr():
stringObject.substr(start,length) 

substr() Method to extract from a string start The specified number of characters from which the subscript begins . The return value is a string , Contains from stringObject Of start( Include start The character referred to ) At the beginning of length Characters . If not specified length, The returned string contains the start To stringObject The ending character of . In addition, if start It's a negative number , It means starting from the end of the string .

var str = 'abcdefg';
console.log(str.substr(1, 3)) // return bcd
console.log(str.substr(2)) // return cdefg
console.log(str.substr(-2, 4)) // return fg, If the target is long , Subject to the actual length of interception

4. Other methods

(1) replace() Method :
stringObject.replace(regexp/substr,replacement) 

replace() Method is used for string replacement operations , It can take two parameters , The former is the substring to be replaced ( It can be regular ), The latter is the replacement text .

If the first parameter passes in a substring or a regular expression without global matching , that replace() Method will be replaced only once ( That is to replace the front one ), Returns the result string after one replacement .

var str = 'abcdeabcde';
console.log(str.replace('a', 'A')); // Abcdeabcde
console.log(str.replace(/a/, 'A')); // Abcdeabcde

If the first parameter passes in a globally matched regular expression , that replace() There will be multiple substitutions for eligible substrings , Finally, the result string that has been replaced many times is returned .

var str = 'abcdeabcdeABCDE';
console.log(str.replace(/a/g, 'A')); // AbcdeAbcdeABCDE
console.log(str.replace(/a/gi, '$')); // $bcde$bcde$BCDE
(2) split() Method :
stringObject.split(separator,howmany)

split() Method is used to split a string into an array of strings . The first parameter separator Represents the split position ( References ), The second parameter howmany Represents the maximum allowed length of the returned array ( In general, it is not set ).

var str = 'a|b|c|d|e';
console.log(str.split('|')); // ["a", "b", "c", "d", "e"]
console.log(str.split('|', 3)); // ["a", "b", "c"]
console.log(str.split('')); // ["a", "|", "b", "|", "c", "|", "d", "|", "e"]

You can also use regularization for segmentation

var str = 'a1b2c3d4e';
console.log(str.split(/\d/)); // ["a", "b", "c", "d", "e"]
(3) toLowerCase() and toUpperCase():
stringObject.toLowerCase()
stringObject.toUpperCase()

toLowerCase() Method to convert uppercase letters in a string to lowercase ,toUpperCase() Method to convert lowercase letters in a string to uppercase letters .

var str = 'JavaScript';
console.log(str.toLowerCase()); // javascript
console.log(str.toUpperCase()); // JAVASCRIPT
(4)trim():
stringObject.trim()

trim() The method is to remove the space around the string , But you can't get rid of the spaces inside the string .

var str = ' aa b ';
console.log(str.trim()); // 'aa b'
(5)concat():
stringObject.concat(stringX,stringX,...,stringX)

concat() Method will convert all its parameters to a string , Then connect to the string in order stringObject Tail of , And return the connected string . Please note that ,stringObject It's not changed .

var str1="Hello ";
var str2="world!";
console.log(str1.concat(str2)); // Hello world!

Tips : Please note that , Use " + " Operators are usually easier to join strings .

(6)sub():
stringObject.sub()

sub() Method is used to display a string as a subscript .

var str="Hello world!";
console.log("<p>Subscript: " + str.sub() + "</p>"); // Subscript: Hello World!
(7)sup():
stringObject.sup()

sup() Method is used to display a string as a superscript .

var str="Hello world!";
console.log("<p>Supscript: " + str.sup() + "</p>"); // Supscript: Hello world!

JQuery String operation of more related articles

  1. JQUERY Selection and operation DOM Elements ( Using regular expressions to match a part of a string )

    JQUERY Selection and operation DOM Elements ( Using regular expressions to match a part of a string ) 1. Match the beginning of the property $("[attributeName^='value']"); 2. Match the end of the property ...

  2. jquery String manipulation

    Purpose : Use what you often use jquery To summarize w3cSchool About js String collation : You can check it when you need it http://www.w3school.com.cn/js/jsref_obj_stri ...

  3. Decrypt jQuery kernel DOM The core function of operation domManip

    domManip What is it? dom namely Dom Elements ,Manip yes Manipulate Abbreviation , Together is Dom Operation means . .domManip() yes jQuery DOM The core function of operation The parameters of encapsulated node operation are given ...

  4. Decrypt jQuery kernel DOM The heart of the operation buildFragment

    What is the document fragment http://www.w3.org/TR/REC-DOM-Level-1/level-one-core.html#ID-B63ED1A3 DocumentFragment is a & ...

  5. Decrypt jQuery kernel DOM operation

    jQuery in the light of DOM The insertion method of operation is about 10 Kind of append.prepend.before.after.replaceWith appendTo.prependTo.insertBefore.in ...

  6. js,jQuery Summary of common array operations

    One .js Summary of common operations in array (1) shift: Delete the first item of the original array , And returns the value of the deleted element : Returns if the array is empty undefined var a = [1,2,3,4,5]; var b = a.shift() ...

  7. 【JAVAWEB Learning notes 】28_jqueryAjax:json data structure 、jquery Of ajax Operation and form verification plug-in

    Ajax-jqueryAjax Today's content : 1.json data structure ( a key ) 2.jquery Of ajax operation ( a key ) 3.jquery The plug-in uses   One .json data structure 1. What is? json JSON(J ...

  8. jQuery The document operation

    jQuery The document operation 1.jq Document structure var $sup = $('.sup'); $sup.children(); // sup All the children $sup.parent(); // sup The father of ( One , ...

  9. python The whole development of the stack ,Day54(jQuery Property operations for , Use jQuery operation input Of value value ,jQuery Document operations for )

    Yesterday's review jQuery Purpose :write less do more Namely js The library of , It is javascript Based on the encapsulation of a framework In the front end , One js File is a module One . usage : 1. Introduction package 2. Enter into ...

Random recommendation

  1. web Front end Basics Dom

    Document object model (Document Object Model,DOM) It's a HTML and XML Documentation programming interface . It provides a structured representation of documents , You can change the content and presentation of the document . What we are most concerned about is ,DOM hold ...

  2. 【 Weight line tree 】bzoj3224 Tyvj 1728 Ordinary equilibrium tree

    A board . #include<cstdio> #include<algorithm> using namespace std; #define N 100001 struct ...

  3. Win10 AppBar

    <Page.BottomAppBar> <CommandBar x:Name="cmdBar" Background="Transparent" ...

  4. mysql Use the basics sql sentence ( One )

    csdn Blog address :mysql Use the basics sql sentence ( One )   Click to enter Command line input mysql -u root -p, Enter and enter the password , Get into mysql. The terminal command ends a statement with a semicolon , It can be divided into multiple lines of input , just ...

  5. There's a picture, there's a truth —— About “ Video album : Zero base learning C Language ”

  6. Clr core

    http://hllvm.group.iteye.com/group/topic/43559

  7. networkRequest

    void TPHttp::applyResource(){    QJsonObject json;    json.insert("ProductCode","MoVd ...

  8. js:jquery multiSelect Multiple selection drop-down box instance

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. SPOJ.TLE - Time Limit Exceeded(DP High dimensional prefixes and )

    Topic link \(Description\) Given a length of \(n\) Array of \(c_i\) and \(m\), Long for \(n\) Sequence \(a_i\) Number , Satisfy :\(c_i\not\mid a_i,\quad a_i\& ...

  10. Applet user openid Set cache

    wx.setStorageSync('openid', res.data.data.openid), Set up     var openid = wx.getStorageSync('openid') obtain