String objects and methods in JavaScript

Source: Internet
Author: User

String Object

Two types of String objects are created

var a= "Hello";

var b=new String ("Hello");

Here's how

CharAt () Find characters in a string based on subscript

    Alert (A.charat (3));//Returns a character string according to the subscript

    Alert (A.charat (10));//empty, cannot find this subscript

charCodeAt () returns the character at the specified position

var a= "Hello world! Hello world! ";

var d= "This is a string";

Alert (A.charcodeat (1));//101

    Alert (D.charcodeat (3));//26465

String.fromCharCode () returns a string using Unicode encoding

     Alert (String.fromCharCode (65,66,67));//

Concat () is used to concatenate one or more strings without altering the original array

var a= "Hello world! Hello world! ";

var b= "Sunshine Incense burner to give birth to purple smoke";

var c= "Less than mountain send Me Love";

Alert (A.concat (B,C))

 Search () finds the string to match, returns the first matching subscript if found, returns 1 if not found

    var c= "123456789";

     Alert (C.search ("567"));//4

     Alert (C.search ("5671"));//-1

 Replace () replaces other characters with some words

     var d= "Xiao Huang is a dog, small yellow is very handsome";

     var e=d.replace ("Small yellow", "small Black");//small yellow to small black, only change for the first time

    var e=d.replace (/small yellow/g, "small black")//change within the string

    Alert (e);

Split () splits a string into an array of strings, and the original array is unchanged

     var a= "Hello world!";

Alert (A.split (""));

Alert (A.split ("O"));//hell, W,rld

  IndexOf () returns the position of the first occurrence of a specified character in a string

    var a= "Hello world!";

    Alert (A.indexof ("O"));//4

    Alert (A.indexof ("P"));//-1 No search-1

LastIndexOf () Search from backward forward

    var a= "Hello world!";

Alert (A.lastindexof ("O"));//7

Match ()

var a= "Hello world! Hello world! ";

Alert (' World ')//Find the first one without looking for 5

Alert (A.match (/world/g));//Regular expression (global search)

Slice () extracts a part of a string that can be negative

var a= "Hello world! Hello world! ";

Alert (A.slice (5,13));//To the first subscript, not the second subscript

Alert (A.slice ( -15,-7));

//substring () same as slice, but does not accept negative numbers

var a= "Hello world! Hello world! ";

Alert (a.substring (4,12));

Alert (a.substring ( -3,-1));//There's nothing.

SUBSTR () intercepts the specified number of characters starting from the specified subscript

var a= "Hello world! Hello world! ";

Alert (A.SUBSTR (4,6));//intercept 6 from Subscript 4

toLowerCase () Converts a string to lowercase

var a= "Hello world! Hello world! ";

Alert (A.tolowercase ());

toUpperCase () Converts a string to uppercase

var a= "Hello world! Hello world! ";

Alert (A.touppercase ());

String objects and methods in JavaScript

Related Article

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.