当前位置:网站首页>JS string method
JS string method
2022-06-24 18:35:00 【Brother Mengfan】
Catalog
1、indexof()
var str1 = 'hello world';
var p = str1.indexOf('l');
console.log(p); //2
p = str1.indexOf('l', 3);
console.log(p); //3
p = str1.indexOf('l', 4);
console.log(p); //9
p = str1.indexOf('llo');
console.log(p); //2
p = str1.indexOf('lloo');
console.log(p); //-1Be careful : stay indexof() The parameters of the , It can be a single character , Find it and return to its first position , Go back if you can't find it -1.
2、charAt()
var str = 'hello world';
var c = str.charAt(7);
console.log(c); //o
c = str.charAt(-1);
console.log(c); // No output Don't complain
c = str.charAt(20);
console.log(c); // No output Don't complain Be careful : When the specified index value is not within the range of the string , No mistakes , But there will be no output .
3、concat()
var str = 'hello';
var s = str.concat(' world', '!');
console.log(s); //hello world!Be careful :
(1) Use concat() To splice strings is the same as using the plus sign to splice strings , But use concat() A little more elegant .
(2) But it should be avoided in development , Used especially in loops ( Memory footprint ).
4、substring()
var str = 'charset';
console.log(str);
var s = str.substring(1); // harset Intercept from subscript to 1 To the end of the string
console.log(s);
s = str.substring(1, 5);
console.log(s); //hars Left closed right away
s = str.substring(1, 3);
console.log(s); // ha
s = str.substring(3, 1);
console.log(s); // ha be equal to s = str.substring(1, 3);
s = str.substring(-1);
console.log(s); // If the first argument given is a negative number , Then it will copy the string and return .explain :
(1) If only the first parameter is specified , Then the substring will run from the specified position to the end ;
(2). If both parameters are specified , Then the substring will change from the start position to the character before the end position ;( Left closed right away )
5、substr()
var str = 'charset';
console.log(str);
var s = str.substr(1, 2);
console.log(s); //ha
s = str.substr(4, 2);
console.log(s); //se From the index number to 4 Intercept 2 position
s = str.substr(2);
console.log(s); // arset Cut to the end
s = str.substr(-2);
console.log(s); // et Return to intercept Be careful :
(1) The parameter of this method is from that position , How many characters to intercept .
(2) If both parameters are specified , Then the character of the specified length is intercepted from the specified position to form a new word
6、slice()
var str = 'The morning is upon us.';
console.log(str);
var s = str.slice(5);
console.log(s); //orning is upon us.
s = str.slice(5, 9);
console.log(s); //orni
s = str.slice(5, -3);
console.log(s); //orning is upon
s = str.slice(-1, 5);
console.log(s); // No output content explain :
(1) The function of this method and substr() Similar function of , Are used to intercept substrings .
(2) If only the first parameter is specified , From the specified position to the end of the original string .
(3) If you specify two parameters , Characters from the specified position to the end position will be extracted .
(4) If the second parameter is negative , The character from the specified position to the end of the number from the back to the front will be extracted .
(5) If the first argument is negative , There is no output , No mistake. .
7、replace()

var str = 'Twas the night before Xmas...';
console.log(str);
// Put the letters e Replace with ,
var s = str.replace('e', ',');
console.log(s); //Twas th, night before Xmas... Only one was replaced
s = str.replace(/\s+/ig, '-');
console.log(s); //Twas-the-night-before-Xmas... Replaced all spaces Be careful :
8、replaceAll()
var str = 'TwAs the night before Xmas...';
console.log(str);
var s = str.replaceAll('as', ',');
console.log(s); //TwAs the night before Xm,...
s = str.replaceAll(/\s+/g, '-');
console.log(s); //TwAs-the-night-before-Xmas...
s = str.replaceAll(/as/ig, ',');
console.log(s); //Tw, the night before Xm,...Be careful :
(1)replaceAll() Method will replace all found characters ;
(2)replaceAll() Method if you want to use regular expressions , Then you must add a global match parameter (/g).
9、split()
var str = "Hello World How are you doing";
console.log(str);
var arr = str.split(' ');
console.log(arr);
arr = str.split(/\s+/);
console.log(arr);
str = "Hello,World,How,are,you,doing";
console.log(str);
arr = str.split(',');
console.log(arr);explain :
(1)split() Method cuts the string according to the specified delimiter , Generate an array ;
(2)split() Method supports regular expressions to cut .
10、trim()
var str = ' char set ';
console.log(str);
var s = str.trim();
console.log(s); //char set There is no space before or after The space in the middle cannot be removed Be careful : Only the front and back spaces can be deleted , The space in the middle cannot be removed .
11、match()
match() Method returns the result of a string matching the regular expression .
var str = 'For more information, see Chapter 3.4.5.1';
console.log(str); // Define a regular expression
var regex = /see (chapter \d+(\.\d)*)/i;
var flag = str.match(regex);
console.log(flag);边栏推荐
- 【leetcode】838. Push domino (Analog)
- Implementation of pure three-layer container network based on BGP
- RestCloud ETL抽取动态库表数据实践
- Millions of dollars worth of NFT were stolen in the attack, and Google issued an emergency warning to 3.2 billion users worldwide | February 21 global network security hotspot
- Data modeling technology of Business Intelligence BI
- Rapidssl getting started SSL certificate
- [JS Framework] Failed to execute the callback function:
- 你知道CMDB吗?
- Some knowledge of the beginning of 2022
- 2022 network security C module of the secondary vocational group scans the script of the surviving target aircraft (municipal, provincial and national)
猜你喜欢

SAP license:sap s/4hana is the answer

微服务系统设计——子服务项目构建

解决执行MapReduce程序控制台没有日志信息WARN Please initialize the log4j system properly
Ultimate Guide: comprehensive analysis of log analysis architecture of Enterprise Cloud native PAAS platform

Analysis on the issue of raising the right of MSSQL in 2021 secondary vocational group network security competition in Shandong Province

The country has made a move! Launch network security review on HowNet

C language - structure II

Mariana Trench, Facebook's open source code analysis tool

Get the actual name of the method parameter through the parameter

Specification for self test requirements of program developers
随机推荐
Gateway solves cross domain access
Monotone stack template
C language - structure II
SAP license: SAP s/4 Hana module function introduction
Complete Guide to web application penetration testing
What are the grades of financial products?
你知道CMDB吗?
EasyNVR使用Onvif探测设备失败,显示“无数据”是什么原因?
25.sql statement differentiation
On the principle of cloud streaming multi person interaction technology
Paper sharing | self supervised learning paper jointly released by Yann Lecun and read by engineers
Exception: Gradle task assembleDebug failed with exit code 1
【你真的会用ES吗】ES基础介绍(一)
Huitongda officially landed at the Hong Kong Stock Exchange: the gross profit margin continued to decline, and the book value of several shareholders still suffered losses
[North Asia data recovery]_ mdb_ catalog. Mongodb database data recovery case in case of WT file corruption
Some knowledge of the beginning of 2022
It is often blocked by R & D and operation? You need to master the 8 steps before realizing the requirements
Business based precipitation component = & gt; manage-table
Selection (032) - what is the output of the following code?
Project Management Guide: tips, strategies and specific practices