JavaScript usage (eight)

Source: Internet
Author: User
Tags locale

1.JavaScript String

JavaScript strings are used to store and manipulate text.

A string can store a series of characters, such as "John Doe."

The string can be any character inserted into the quotation marks. You can use single or double quotation marks:

var " Volvo XC60 ";

You can use the index location to access each character in a string:

var character = carname[7];

The index of a string starts at 0, which means that the first character index value is [0], the second is [1], and so on.

You can use quotation marks in a string, and the quotation marks in the string do not match the quotation marks of the string:

var " It ' s alright " ; var " He is called ' Johnny ' " ; var ' He is called "Johnny" ';

You can also add escape characters to the string using quotation marks:

var ' it\ ' s alright ' ; var " He is called \ "johnny\" ";

1.1 String length

You can use the built-in property length to calculate the string lengths:

var " abcdefghijklmnopqrstuvwxyz " ; var sln = txt.length;

1.2 Special characters

In JavaScript, strings are written in single or double quotation marks.

Because of this, the following instance of JavaScript cannot be resolved:

"Vikings" from the North .  "

The string "We are the so-called" is truncated.

How to solve the above problems? You can use a backslash (\) to escape the double quotation marks in the "Vikings" string, as follows:

" We are the so-called \ ' Vikings\ ' from the north. "

A backslash is an escape character . The escape character converts a special character to a string character:

The escape character (\) can be used to escape apostrophes, line breaks, quotation marks, and other special characters.

The following table lists special characters that can be escaped with escape characters in a string:

Code Output
\‘ Single quotation marks
\" Double quotes
\\ Back slash
\ n Line break
\ r Enter
\ t tab (TAB)
\b Backspace
\f Page break

1.3 The string can be an object

Typically, the JavaScript string is the original value and can be created with characters: var firstName = "John"

But we can also use the New keyword to define a string as an object: var firstName = new String ("John")

var " John " ; var New String ("John"); typeof // returns a String typeof // return Object

Do not create a String object. It slows down execution speed and may have other side effects:

var " John " ;               var New String ("John"//  result is false because X is a string and Y is the object

= = = is absolutely equal, that is, the data type and value must be equal.

1.4 String properties and methods

Raw value strings, such as "John", have no properties and methods (because they are not objects).

Raw values can use JavaScript properties and methods, because JavaScript can take raw values as objects when executing methods and properties.

1.4.1 String Property

Properties Description
Constructor Returns a function that creates a string property
Length Returns the length of a string
Prototype Allows you to add properties and methods to an object

1.4.2 String method

Method Description
CharAt () Returns the character of the specified index position
charCodeAt () Returns the Unicode value of the specified index position character
Concat () Concatenate two or more strings, returning the concatenated string
fromCharCode () Convert Unicode to a string
IndexOf () Retrieves the position of the first occurrence of the specified character in a string
LastIndexOf () Retrieves the position of the last occurrence of the specified character in a string
Localecompare () Compare two strings in a local-specific order
Match () Found a match for one or more regular expressions
Replace () To replace a substring that matches a regular expression
Search () Retrieving a value that matches a regular expression
Slice () Extracts a fragment of a string and returns the extracted part in a new string
Split () To split a string into a substring array
SUBSTR () Extracts a specified number of characters from the starting index number in a string
SUBSTRING () Extracts the characters between two specified index numbers in a string
toLocaleLowerCase () Converts a string to lowercase based on the host's locale, with only a few languages (such as Turkish) with a local case-specific mapping
toLocaleUpperCase () Converts a string to uppercase based on the host's locale, with only a few languages (such as Turkish) with a local case-specific mapping
toLowerCase () Convert a string to lowercase
ToString () Returns the String object value
toUpperCase () Convert a string to uppercase
Trim () Remove string end and end blanks
ValueOf () Returns the original value of a string object

JavaScript usage (eight)

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.