Common handling of JavaScript string objects

Source: Internet
Author: User

In front-end development, we often face a variety of operations, processing, where string processing is very high, and some developers will even convert other data types into string data type, after the operation, in turn back, it is good, there are disadvantages, and now look at the string has what to do with the way, There's a wrong place, welcome to correct me.

String object property Length

Definition: The Length property can return character lengths of a string

Console.log ("ASDASD". Length)  //6  
Constructor

Definition: The reference to the function that created the object, is not clear, is to return the function that created the object, the string is generally the return string () function

Console.log ("asd123". Constructor) //function String ()
Prototype

Prototype is defined on the W3schol: Allows you to add properties and methods to the object, however, the string object has this property, seemingly no, it seems that only the function can call this property, other objects can not be called, this property should be combined with JavaScript prototype object to see , there's something you want to see.

Console.log ("asd123". prototype) //undefined
Console.log (function () {}.prototype) //object ()

Method Charat () of the String object

Definition: The CharAt () method returns the character at the specified position

var txt= "ASD I See"Txt.charat (3) //I

charCodeAt ()

Definition: Find character Unicode encoding at specified position

var txt= "ASD Mine" txt.charcodeat (3) //25105  

fromCharCode ()

Definition: Turning Unicode encoding into characters

Console.log (String.fromCharCode (25105)) //I

SUBSTR ()

Definition: Extracts a specified number of characters from the starting index number in a string.
Syntax String.substr (start,length); start: The index value of the start position, length: Extract the lengths of the strings

var txt= "HELLO" Txt.substr (0,3)  //hel

SUBSTRING ()

Definition: Extracts the characters between two specified index numbers in a string.
Syntax string.substring (from,to); The index value of the From: Start position, to: (optional parameter) end position, if not written, is extracted to the last

var txt= "Hello World" txt.substring (0,3)  //hel//3-0=3 So is extracting 3 characters

Slice ()

Definition: Extracts the characters between two specified index numbers in a string
Syntax String.slice (start,end); 00 parameter start: Index value of the start position, end: (optional parameter) the index of the ending position, if not written, is extracted to the last

var txt= "Hello World" Txt.slice (0,3)  //hel//3-0=3 So is extracting 3 characters

The difference between this method and the substring is that his argument can be negative, and substring will default to a negative number of 0.

The difference between var txto= "and substring"
The difference between Console.log (txto.substring ( -1)) //and substring
Console.log (Txto.slice ( -1)) //Don't

//

Console.log (Txto.slice (0,-1)) //and substring District

IndexOf ()

Definition: Returns the position of the first occurrence of a specified string value in a string
Syntax String.IndexOf (value,index); parameter value: The string to retrieve, index (optional argument): The location to start retrieving, if not, by default starting from the beginning

var txt= "Hellohei" Console.log (Txt.indexof ("H")) //5

LastIndexOf ()

Definition: Returns the location of the last occurrence of a specified string value
Syntax String.LastIndexOf (value,start); parameter value: The string to retrieve, start (optional argument): The location to start retrieving, if not, the default starting from the back

var txt= "I speak Chinese in China" Console.log (Txt.lastindexof ("China")) //5  

Split ()

Definition: Splitting a string into an array of strings
Syntax String.Split (separator,limit), parameter separator (optional parameter): A string or regular expression, cut from the position of the parameter, if not undivided, limit (optional parameter): Returns the length of the array, if not, all cuts

var txt= "My Parameters Tangent" Console.log (Txt.split ("")) //["I", "the", "parameter", "number", "cut"]
Console.log (Txt.split ()) //["My parameter Cut"]

Concat ()

Definition: Link two strings or multiple
Syntax String.Concat (string1, string2, ..., stringx); argument string1: linked string, not less than one

var txt1= "my" var txt2= "knife" var txt3= "Kill" Console.log (Txt1.concat (TXT2,TXT3)) //My Knife kills
Console.log (Txt1.concat (TXT2)) //My knife
It's mine

Trim ()

Definition: Clear whitespace on both sides of the string

Console.log ("My old songs are  broken  ". Trim ())  //My old songs are broken

toUpperCase ()

Definition: Converting a string to uppercase

Console.log ("asd321". toUpperCase ()) //asd321  

toLowerCase ()

Definition: Converting a string to lowercase

Console.log ("ADFDasd651". toLowerCase ()) //adfdasd651  

Search ()

Definition: The search () method is used to retrieve substrings, or to retrieve substrings that match a regular expression

var txt= "1HE hre ehe ehh" Console.log (Txt.search ("he") //9
1

Match ()

Definition: the match () method retrieves the specified value within a string, or finds the match of one or more regular expressions

var txt= "1HE hre ehe ehh hee" Console.log (Txt.match ("he") //["he"]//["he", "he", " he"]

Vallueof ()

Definition: Returns the original value of a string object, does not know how to understand, but the effect is like this, seemingly to the string does not play a role

var str= "Hello world!"
Console.log (str.valueof ()) //hello world!

Common handling of JavaScript string objects

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.