Home > Web Front-end > JS Tutorial > body text

A brief introduction to the basic methods of string creation in JavaScript_Basic knowledge

WBOY
Release: 2016-05-16 15:51:13
Original
999 people have browsed it

There are several ways to create a string. The simplest is to enclose a set of characters in quotes, which can be assigned to a string variable.

   var myStr = "Hello, String!";
Copy after login



You can use double quotes or single quotes to enclose a string, but be aware that the pair of quotes that delimit the string must be the same and cannot be mixed.


A statement like var myString = "Fluffy is a pretty cat.'; is illegal.


​​​Allows the use of two kinds of quotation marks, making certain operations simple, such as embedding one type into the other:

 document.write("<img src='img/logo.jpg' height='30' width='100' alt="Logo">");
Copy after login

We created several strings in the above script, but in essence, they are not real string objects. To be precise, they are string type values. To create a string object, use the following statement: var strObj = new String("Hello, String!");


Use the typeof operator to check and you will find that the myStr type above is string, and the strObj type is object.
 
If you want to know the length of a string, use its length property: string.length.


How to get the character at the specified position of the string: string.charAt(index);

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!