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

Summary of character methods and string operation methods in js (with code)

不言
Release: 2018-08-13 17:40:43
Original
1514 people have browsed it

This article brings you a summary of character methods and string operation methods in js (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

Character method

1. charAt()
Receives a parameter, based on the character position of 0. Returns the character at the given position as a single string.

   var stringValue = "hello world";
   console.log(stringValue.charAt(1)); //"e"
Copy after login

2. charCodeAt()
Receives a parameter, based on the character position of 0. What is returned is the character encoding.

   var stringValue = "hello world";
   console.log(stringValue.charCodeAt(1)); //101
Copy after login

String operation method

1. concat()
is used to splice one or more strings together and return the new string obtained by splicing without modifying the string. The value itself just returns a string value of a basic type.

   var stringValue = "hello ";
   var result = stringValue.concat("world");
   console.log(result); // "hello world"
   console.log(stringValue); // "hello"
Copy after login

2. slice()
Intercepts a string and only returns a basic type string value without any impact on the original string.
If two parameters are passed, the first parameter is the starting position of interception, and the second parameter is the ending position of interception.

   var stringValue = "hello world";
   console.log(stringValue.slice(3)); //"lo world"
   console.log(stringValue.slice(3,7)); //"lo w"
Copy after login

3. substring()
Intercepts a string and only returns a basic type string value without any impact on the original string.
If two parameters are passed, the first parameter is the starting position of interception, and the second parameter is the ending position of interception.

   var stringValue = "hello world";
   console.log(stringValue.substring(3)); //"lo world"
   console.log(stringValue.substring(3,7)); //"lo w"
Copy after login

4. substr()
Intercepts a string and only returns a basic type string value without any impact on the original string.
If two parameters are passed, the first parameter is the starting position of interception, and the second parameter is the number of characters returned.

   var stringValue = "hello world";
   console.log(stringValue.substr(3)); //"lo world"
   console.log(stringValue.substr(3,7)); //"lo worl"
Copy after login

Related recommendations:

Detailed analysis and difference comparison of front-end modularization in Js

js implements timestamp conversion to time format Detailed code explanation

The above is the detailed content of Summary of character methods and string operation methods in js (with code). 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!