当前位置:网站首页>String methods and instances
String methods and instances
2022-07-24 05:54:00 【DniNgL】
String method
var str = "VFDrerDAVvszds" // Literal declaration string
// var str1=new String("sssssss") // Constructor mode declaration string
console.log(str.length); // length Property returns the length of the string :13
- indexOf() Method returns the index of the first occurrence of the specified text in a string ( Location )
console.log(str.indexOf("s")) //indexOf() Method returns the index of the first occurrence of the specified text in a string ( Location )10
2.lastIndexOf() Method returns the index of the last occurrence of the specified text in the string
console.log(str.lastIndexOf("s")); // lastIndexOf() Method returns the last occurrence of a specified string value
If the element you are looking for is not found , indexOf() and lastIndexOf() All return to -1
- search() Method to search for a string of specific values , And return to the matching position
console.log(str.search("s")); // Search for a string of specific values , And return to the matching position
- slice(start,end) Extract a part of the string and return the extracted part in the new string
console.log(str.slice(3, 5)); // Crop the position in the string 3 To the position 5 Fragments of :re
- substring(start,end) Extract a part of the string and return the extracted part in the new string ( Cannot accept negative index )
console.log(str.substring(3,5));// Crop the position in the string 3 To the position 5 Fragments of :re
- substr(start, length) from start The index begins to extract several strings of strings
console.log(str.substr(3,5));//rerDA
- .replace() Method replaces the first matching value with another value , Does not change the string that calls it . It returns a new string
console.log(str.replace("V", " you ")); // you FDrerDAVvszds
- toUpperCase() Convert strings to uppercase
console.log(str.toUpperCase());//VFDRERDAVVSZDS
- adopt toLowerCase() Convert string to lowercase
console.log(str.toLocaleLowerCase());//vfdrerdavvszds
- concat() Concatenate two or more strings
var str1 = "hello",str2 = "javaScript"
var str3 = str1.concat(str2)
console.log(str3);//hellojavaScript
- trim() Method to delete the whitespace characters at both ends of a string
var str=" swi "
var str1=str.trim()
console.log(str1);//swi
12.charAt() Method returns the specified subscript in the string ( Location ) String
var str = "HELLO JAVASCRIPT";
str.charAt(0); // return H
- Can pass split() Convert string to array
var txt = "a,b,c,d,e"; // character string
console.log(txt.split(",")); // ["a", "b", "c", "d", "e"] Separate with commas
The following is an example of a string
The string method uses the middle four digits of the mobile phone number * Express
<script>
var str = '12133536409'
function mosaic(str, start, length) {
let left = str.substring(0, start)
// console.log(left); //121
let middle = "*".repeat(length)
// console.log(middle); //****
let right = str.substring(start + length)
// console.log(right);//6409
return left + middle + right // Put the new number return come out
}
console.log(mosaic(str, 3, 4)); //121****6409
</script>
String length truncation
function cut(str, length) {
if (str.length < length) return str
else
return str.substr(0, length) + "..."
}
console.log(cut(" Hello , I'm the monkey king ", 5)); // Hello , I am a ...
Replace
let send = " Dear users [0] Hello! , Welcome to consult our products "
let numlist = "13344445555,12233334444,16677778888"
numlist.split(",").forEach(item => {
console.log(send.replace("[0]", item));
})
The input result is as follows 
边栏推荐
- js星星打分效果
- Points for attention in adding spp module to the network
- likeshop单商户SAAS商城系统搭建,代码开源无加密。
- 《机器学习》(周志华)第一章 绪论 笔记 学习心得
- MySQL batch insert demo
- systemctl + journalctl
- 多商户商城系统功能拆解08讲-平台端商品分类
- 对ArrayList<ArrayList<Double>>排序
- Jupyter notebook选择conda环境
- The method of using bat command to quickly create system restore point
猜你喜欢

主成分分析计算步骤

Typora 安装包2021年11月最后一次免费版本的安装包下载V13.6.1

《机器学习》(周志华) 第3章 线性模型 学习心得 笔记

Chapter IV decision tree summary

What do programmers often mean by API? What are the API types?

《信号与系统》(吴京)部分课后习题答案与解析

Add se channel attention module to the network
![[activiti] activiti introduction](/img/17/bd8f6fd8dd8918a984ca0a3793ec0b.jpg)
[activiti] activiti introduction

【深度学习】手把手教你写“手写数字识别神经网络“,不使用任何框架,纯Numpy

多商户商城系统功能拆解04讲-平台端商家入驻
随机推荐
Chapter III summary of linear model
Positional argument after keyword argument
找数组中出现次数最多的数
快速打开管理工具的命令
多商户商城系统功能拆解04讲-平台端商家入驻
ThreadLocal stores the current login user information
[activiti] activiti system table description
多商户商城系统功能拆解06讲-平台端商家入驻协议
likeshop单商户SAAS商城系统搭建,代码开源无加密。
The female colleague of the company asked me to go to her residence to repair the computer at 11 o'clock at night. It turned out that disk C was popular. Look at my move to fix the female colleague's
IoTP2PGate 两台物联网设备点对点通信快速实现方案
Multi merchant mall system function disassembly lecture 05 - main business categories of platform merchants
Watermelon book / Pumpkin book -- Chapter 1 and 2 Summary
《统计学习方法(第2版)》李航 第22章 无监督学习方法总结 思维导图笔记
How to quickly recover data after MySQL misoperation
opencv读取avi视频报错:number < max_number in function ‘icvExtractPattern
labelme转voc代码中的一个小问题
Subsystem technology and ecology may memorabilia | square one plan launched, Boca launched xcm!
Loss after cosine annealing decay of learning rate
《统计学习方法(第2版)》李航 第14章 聚类方法 思维导图笔记 及 课后习题答案(步骤详细) k-均值 层次聚类 第十四章