JS Operation string

Source: Internet
Author: User
Tags ming

JS Operation string

From:https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/ 001434499203693072018f8878842a9b0011e3ff4e38b6b000
Template string

To concatenate multiple strings, you can connect them with the + sign:

var name = ' Xiao Ming ';
var age = 20;
var message = ' Hello, ' + name + ', you are this year ' + Age + ' years old! ';
alert (message);
If there are many variables that need to be connected, using the + sign is more troublesome. ES6 adds a template string that represents the same method as the multiline string above, but it automatically replaces the variables in the string:
var name = ' Xiao Ming ';
var age = 20;
Alert (' Hello, ${name}, you ${age} year old! ');

var name = ' Xiao Ming ';
var age = 20;
var message = ' Hello, ${name}, you ${age} year old! ';
alert (message);
Exercise: Test whether your browser supports ES6 template strings, and if not, change the template string to a normal string of + connections:

If the browser supports template strings, the variables inside the string will be replaced:

var s = ' Hello, world! ';
S.length; 13

var s = ' Hello, world! ';

S[0]; H
S[6]; ‘ ‘
S[7]; ' W '
S[12]; ‘!‘
S[13]; Undefined out of range index will not error, but return undefined

JavaScript provides some common methods for strings, note that calling these methods does not alter the contents of the original string, but instead returns a new string:


toUpperCase () turns a string all uppercase:

var s = ' Hello ';
S.touppercase (); Return ' HELLO '

toLowerCase () turns a string all lowercase:

var s = ' Hello ';
var lower = S.tolowercase (); Returns ' Hello ' and assigns a value to the variable lower
Lower ' Hello '

IndexOf () searches for the location where the specified string appears:

var s = ' Hello, world ';
S.indexof (' World '); Returns 7
S.indexof (' World '); The specified substring was not found, returned-1

SUBSTRING () returns the substring of the specified index interval:

var s = ' Hello, world '
S.substring (0, 5); Starting from index 0 to 5 (excluding 5), return ' Hello '
S.substring (7); Starting at index 7 to the end, return ' world '

JS Operation string

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.