Home >Web Front-end >JS Tutorial >A brief introduction to the basic methods of string creation in JavaScript_Basic knowledge

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

WBOY
WBOYOriginal
2016-05-16 15:51:131030browse

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!";



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">");

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);

Statement:
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