Home > Web Front-end > JS Tutorial > How to manipulate strings using JavaScript

How to manipulate strings using JavaScript

不言
Release: 2018-12-17 17:28:55
Original
2436 people have browsed it

How to manipulate strings using How to manipulate strings using How to manipulate strings using JavaScript can perform various operations on strings, such as string output, string concatenation, and escape characters. In this article, we will introduce in detail how to operate strings in How to manipulate strings using How to manipulate strings using How to manipulate strings using JavaScript.

How to manipulate strings using How to manipulate strings using How to manipulate strings using JavaScript

1. Use How to manipulate strings using How to manipulate strings using How to manipulate strings using JavaScript to output a string

In How to manipulate strings using How to manipulate strings using How to manipulate strings using JavaScript, you can use ""( Double quotes) or '' (quotes); you can use either.

Let’s take a look at an example

The code is as follows

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>How to manipulate strings using How to manipulate strings using How to manipulate strings using JavaScript</title>
  </head>
  <body>
    <script>
      var x;
      x = "你好,PHP中文网!"
      document.write(x);
    </script>
  </body>
</html>
Copy after login

The running results are as follows

How to manipulate strings using How to manipulate strings using How to manipulate strings using JavaScript

If you If you use single quotes, the above results will also be displayed. You can try it yourself, but I won’t go into it here.

2. Use How to manipulate strings using How to manipulate strings using How to manipulate strings using JavaScript to connect strings

You can use arithmetic operators to connect strings and output them.

Below we use the " " sign to connect strings

The code is as follows

   <script>
  var x;
  x = "你好" + "php" + "中文网";
  document.write(x);
</script>
Copy after login

The operation effect is as follows: three strings are connected together.

How to manipulate strings using How to manipulate strings using JavaScript

3. Escape characters in How to manipulate strings using How to manipulate strings using How to manipulate strings using JavaScript

When writing a program, sometimes you want to start a new line in a string , or want to have spaces. In this case, you can use "\" to represent special characters (escape sequences).

Here are three representative escape characters.

Tab character: \t

The first is the tab character. If you want to add spaces between characters, you can use "\t "

<script>
  document.write(&#39;早安\t晚安&#39;);
</script>
Copy after login

The running results are as follows:

How to manipulate strings using How to manipulate strings using How to manipulate strings using JavaScript

Single quotation mark: \'

The code is as follows

<script>
  document.write(&#39;I\&#39;m happy&#39;);
</script>
Copy after login

The running result is as follows

How to manipulate strings using JavaScript

Newline:\n

If you want to wrap the string, you can use\n(this time Output in console.log)

The code is as follows

<script>
  console.log(&#39;I\&#39;m happy \n You are happy too&#39;);
</script>
Copy after login

The running effect is as follows

How to manipulate strings using JavaScript

The above is the detailed content of How to manipulate strings using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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