JS Basics Comb---String object

Source: Internet
Author: User
Tags first string

A: Declaration of the object

var stringobject   new  String ("Hello word"); var stringobjectb  

Properties of the B:stringobject

Stringobject.length, string length (spaces are also placeholders), and corresponding, can be accessed by string[n] to access the characters of the string with the corner labeled N

var str = "Today is good Today"; Console.log (str.length); //19Console.log (str[2]); // D

String Method ↓

C: Single-character access

CharAt (n), passing in numeric value, returning the corresponding character in that position

charCodeAt (n), passing in numeric value, returns the position corresponding to the character encoding

var str = "Dashu yang"; Console.log (Str.charat (2)); // SConsole.log (Str.charcodeat (2)); //  the

D:slice (n), string intercept, accept one or two values

var str = "Dashu yang"; Console.log (Str.slice (2)); // Shu Yangconsole.log (Str.slice (2,5)); // Shu // ng = = Console.log (Str.slice (str.length-2))

E:indexof (), character position, passed in string, returns the location of the string, and if no 1 is returned, the second argument can be passed, indicating where to start the search

var str = "Dashu yang"; Console.log (Str.slice (' a ')); // 1console.log (Str.slice (' as ')); // 1console.log (str.slice (' X ')); // -1console.log (Str.slice (' A ', 3)); //

F:match (); a pattern-matching method for a string that receives a regular expression or RegExp object, returning an array

var text    = "Cat,bat,sat,fat"=/.at/; var matches = Text.match (pattern); // Same as Pattern.exec (text) Console.log (matches);//catconsole.log (matches[0]);//cat

E:replace (), string substitution, receive two parameters, first argument is a string or regular expression, the second argument is a string or a function, returns a string

var text    = "Cat,bat,sat,fat", Console.log (Text.replace (' Ca ', ' 123 '));   123t,bat,sat,fatconsole.log (Text.replace (/at/g, ' 123 ')); // c123,b123,s123,f123

F:split (), string split, receive one or two arguments, the first string or regular expression, the second is the maximum length of the returned array, the return value is an array

var text    = "Cat,bat,sat,fat"; Console.log (Text.split (', '));   ["Cat", "bat", "sat", "fat"]console.log (Text.split (', ', ' 3 ')); // ["Cat", "bat", "sat"]Console.log (Text.split (/at/,)); // ["C", ", B", ", S", ", F", "" ")

JS Basics Comb---String object

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.