Home  >  Article  >  Web Front-end  >  How to use string objects in JavaScript

How to use string objects in JavaScript

不言
不言Original
2018-12-11 15:32:003510browse

How to use string objects in JavaScript: First use new to create a string object; then give a suitable object name; finally pass the string "hello world" as a parameter and output it.

How to use string objects in JavaScript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

Built-in functions refer to objects prepared in advance by JavaScript. The String object is a built-in function that can be used to process strings. The following article will introduce to you the use of string objects in JavaScript. method.

First of all, let’s take a brief look at the representative built-in functions

String: Processing strings

Number: Processing numbers

Boolean: Processing logical values

Array: Processing arrays

Date: Processing dates

Math: Processing through numerical calculations

Built-in function represents starting from the beginning The content of the function has already been determined, so there is no need to define it again, just execute it, which is very convenient.

When using built-in objects, we use new to create objects, as shown below.

var 对象名称= new object();

Let’s actuallyCreate a string object

We give the appropriate object name and use the string hello world as a parameter Pass it and then output it

var s = new String("hello world");

Note: Don’t forget to enclose it with "" or '' when outputting the string.

Let’s look at the specific code

 <!DOCTYPE html>
<html>
</body>
</html>
<head>
<meta charset="utf-8">
<title>JavaScript</title>
</head>
<body>
<script>
  var s=new String("hello world");
</script>
</body>
</html>

After creation, let’s call it using console.log.

Let’s call the properties of the string object

The properties of the string object have the following three properties

Attribute 1 .length (the output length of the string)

Confirm the calling method of the object property.

If you continue to use the attribute name with "." in the variable name, you can call the attribute value.

<script>
var s = new String(&quot;hello world&quot;);
console.log(s.length);
</script>

You can see the following figure in the console

How to use string objects in JavaScript

Since the number of characters in hello world is 11 characters, the output is " 11". (Spaces are also included)

Attribute 2. charAt (get the number of characters from the left and output it)

<script>
var s=new String("hello world");
console.log(s.charAt(1));
</script>

You can see it in the console as shown below

How to use string objects in JavaScript

#We specified the first character, so it appears as e. (The number of characters starts from 0)

Attribute 3.replace (replace character)

<script>
var s=new String("hello world");
console.log(s.replace("world","php中文网"));
</script>

You can see it in the console as shown below

How to use string objects in JavaScript

replace can replace the string, and world is replaced by php Chinese network.

The above is the detailed content of How to use string objects in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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