Home  >  Article  >  Web Front-end  >  Summary of character methods and string operation methods in js (with code)

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

不言
不言Original
2018-08-13 17:40:431509browse

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"

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

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"

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"

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"

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"

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!

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