当前位置:网站首页>JS string method
JS string method
2022-06-13 08:37:00 【Medlar plus】
All string methods will return a new string , Instead of changing the original string .
Find string
indexOf(src,index)
Traverse from the beginning target character string , Find a match src The first occurrence of the index ( Location ), No return found -1
lastIndexOf(src,index)
Traverse from the end target character string , Find a match src The first occurrence of the index ( Location ), No return found -1
search(src)
and indexOf identical , It just doesn't have a second start position parameter .
String extraction
slice(start, end)
Extract from start Start to end The string content of , If end Greater than the total length of the string or omitted , Then extract start All subsequent strings , If a parameter is negative ( Negative values are not suitable for IE8 And earlier ), Then count from the end of the string .
var str = "Apple, Banana, Mango";
var res = str.slice(-13,-7); //Banana
substring(start, end)
Be similar to slice(), However, input parameters with negative values are not accepted
substr(start, length)
Be similar to slice, The difference is that the second parameter is the length of the extracted part , If omitted , Is extracted directly to the end of the string ,start Support is negative .
charAt(index)
Returns the specified subscript in the string ( Location ) String , Compared with direct use str[index] The way to get characters is more secure , If it cannot be found, it will directly return an empty string , however str[index] Will return undefined, Simultaneous adoption str[index] Just read the string , Not through str[index] = ‘A’ Change the way index The value of the location .
charCodeAt()
Returns the... Of the specified index character in a string unicode code ;
String segmentation
split(str)
according to str Split strings into arrays , Omit separator , The returned array will contain index [0] The entire string in . If the separator is “”, The returned array will be an array separated by a single character
var str = "Hello";
var arr = str.split(""); ['H','e','l','l','o']
var str = "a,b,c,d,e,f";
var arr = str.split(); // arr[0] by "a,b,c,d,e,f"
String splicing
concat(str1, str2, str3…)
Concatenate multiple strings , It can also be used. + Operator implementation
Other functions
replace(target, src)
In the string target Replace the string with src character string , Only the first match is replaced and is not case sensitive , But the first argument supports regular expressions , That is, if you want to implement case insensitive replacement or replace all matching items , It can be solved by regularization .
str = "Please visit Microsoft!";
var n = str.replace(/MICROSOFT/i, "W3School");
var n = str.replace(/Microsoft/g, "W3School");
toUpperCase()
String capitalization
toLowerCase()
String to lowercase
trim()
Remove white space on both sides ,IE8 This method is not supported in versions and below . Use replace It can also be implemented in combination with regularization , But it's relatively troublesome .
Summary reference :https://www.w3school.com.cn/js/js_string_methods.asp
边栏推荐
- Remote access and control
- Buffer Overflow Vulnerability Lab
- How about a well-known food material distribution information management system?
- 微服务项目搭建二:数据库设计
- Form exercise 2
- Buuctf web (III)
- 0.一些自己初学Solidworks的疑惑
- Notes on development experience: TP5 exp query, SQL analysis, JQ, applet, right-click menu, Linux skills, shell skills, mysql, etc
- 2020-12-28
- Use of addeventlistener in JS
猜你喜欢

How app inventor accesses resource files in assets directory

Svg text stroke effect

d3.js&nvd3. JS - how to set the y-axis range - d3 js & nvd3. js — How to set y-axis range

0.一些自己初學Solidworks的疑惑

Form exercise 2

redis. exceptions. ConnectionError: Error 111 connecting to 172.16.8.128:6379. Connection refused.

Process and scheduled task management

Maternal and infant supplies wholesale industry uses management software to improve efficiency and realize cost reduction and efficiency increase

MySQL parsing serialized fields

Is signed or unsigned selected to create an integer field in MySQL? The answer is as follows:
随机推荐
JS to download files in batches
关于redis使用分布式锁的封装工具类
Buuctf web (VII)
Mysql_ Preliminary summary of database data (Continued)
regular expression
Microservice Project Construction II: database design
Notes on development experience: TP5 exp query, SQL analysis, JQ, applet, right-click menu, Linux skills, shell skills, mysql, etc
Guidance process and service control
Buuctf web (V)
Verify the word limit of textarea input box. Only prompt but no submission limit
2021-04-16
4、 Js-es5-i / O
Daffodil upgrade (self idempotent)
LVM management exercise
Journal d'acquisition du faisceau de fichiers à Elk
DHCP principle and configuration
ERP basic data Kingdee
抖音关键词搜索列表接口,超详细的接口对接步骤
JS - print 99 multiplication table of the for cycle case
Founder of Starbucks: no longer open "public toilets" to non store consumers for safety reasons