JavaScript字符串类型属性和方法

2022/4/17 9:15:50

本文主要是介绍JavaScript字符串类型属性和方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

属性:

length :获取字符串的长度

JS
// 获取字符串的长度
let str = 'hello';
let len = str.length;
console.log(len);	// 5

通过索引值index访问字符串中的每个字符 索引值index从 0 开始

JS
// 通过索引值获取字符串中的某个字符
console.log(str[1]); // e
console.log(str[2]); // l

方法:

charAt( )

说明:返回指定索引位置的字符

JS
let ziFu = 'hello';
console.log(ziFu.charAt(1));
// e

concat( ) 

说明: 连接两个或多个字符串,返回连接后的字符串

JS
let str1 = 'hello',
    str2 = 'world',
    str3 = '你好';
let res = str1.concat(str2,str3,str2,str3);
console.log(res);
console.log(str1);
// helloworld你好world你好
// hello

indexOf( )

说明:返回字符串中检索指定字符第一次出现的位置,如果不存在返回 -1

JS
let str = '2helloa world 123';
let first = str.indexOf('a');
console.log(`a第一次出现的位置是${first}`); 
// a第一次出现的位置是6
JS
// 应用1: 通过这个方法可以判断字符串中的某个字符是否只出现一次   第一次出现位置的索引值和最后一次出现位置的索引值是相等的 
let first1 = str.indexOf('2');// 第一次出现的位置
let last = str.lastIndexOf('2');//第二次出现的位置
let first2 = str.indexOf('world');//
console.log(first1,last,first2);
// 0 15 8

lastIndexOf( )

说明:返回字符串中检索指定字符最后一次出现的位置,如果不存在返回 -1

JS
//indexOf()  lastIndexOf() 如果检索的字符不存在 则返回 -1 ; 
//应用2:通过这两个方法可以判断字符串中是否存在指定的字符 , 存在返回索引值,不存在 返回 -1
 let first = str.indexOf('Q') ;
 let last = str.lastIndexOf('Q') ;
 console.log(first, last);//-1 -1

slice( )

说明:提取字符串的片断,并在新的字符串中返回被提取的部分

JS
// slice(startIndex, endIndex) (截取字符串)	提取字符串的片断,并在新的字符串中返回被提取的部分
let html = 'hello world';
// 1.包前不包后
// 包前(包含起点索引值位置的字符)不包后 (不包含终点索引值位置的字符)
let test = html.slice(0,3)
console.log(test);//hel
// 2.一个参数: 意味着从起点索引值位置开始向后截取整个字符串
let test2 = html.slice(3);
console.log(test2);//lo world
// 3.能接受负数作为参数:意味着倒着截取字符串
let test3 =  html.slice(-5);
console.log(test3);//world
let test4 =  html.slice(-5,-2);
console.log(test4);//wor
// 4.终点索引值超出字符串的长度,则意味着 从起点索引值位置开始向后截取整个字符串
let test5 = html.slice(8,20);
console.log(test5);//rld
// 5.起点是正值,终点是负值  , 意味着截取起点和终点之间所有的字符串(不需要考虑字符串的长度)
let test6= html.slice(8,-2);
console.log(test6);//r
// 6.起点超出字符串的长度,则截取失败
let test7 = html.slice(10,-2);
console.log(test7);

substr( )

substr(startIndex, length)

startIndex: 截取的起点索引值 必选项

length: 截取的长度 可选项

说明:截取指定长度的字符串,得到的结果是截取后的字符串

JS
let html = 'hello world';
// 1. 从指定的索引值为开始截取指定长度的字符串
let test8 = html.substr(2,3)
console.log(test8);//llo
// 2.一个参数: 从起点开始向后截取整个字符串
let test9 = html.substr(3);
console.log(test9);//lo world
// 3.可以接受负数, 意味着倒着截取字符串
let test10 = html.substr(-10,3);
console.log(test10);//ell
// 4.指定的长度超出字符串长度,从指定的索引值为开始截取整个字符串
let test11 = html.substr(4,20);
console.log(test11);//o world

substring( )

substring(startIndex, endIndex) 截取字符串,得到的结果是截取后的新字符串
startIndex: 截取字符串起点的索引值, 必选项
endIndex: 截取字符串终点的索引值 , 可选项

说明:提取字符串中两个指定的索引号之间的字符

JS
// 1. 截取字符串: 包前(包含起点索引值位置的字符)不包后 (不包含终点索引值位置的字符)
let test12 = html.substring(0,3);
console.log(test12);//hel
// 2. 一个参数:意味着从起点索引值位置开始向后截取整个字符串
let test13 = html.substring(2);
console.log(test13);//llo world
// 3.不能接受负数
let test14 = html.substring(-10);
console.log(test14);//hello world
// 4.终点索引值超出字符串的长度,则意味着 从起点索引值位置开始向后截取整个字符串
let test15 = html.substring(9, 20);
console.log(test15);//ld

split( )

说明:split(参数) 把字符串分割为子字符串数组

参数: 参数是分割的标识,也可以接受一个正则表达式作为参数

JS
var tit = 'hello abc world';
//传入具体的字符串,则以该字符串作为分割的标识, 分割后生成的数组中不包含传入的字符串
var test16 = tit.split('o');
console.log(test16);
//[ 'hell', ' abc w', 'rld' ]

var test17 =tit.split('abc');
console.log(test17);
//[ 'hello ', ' world' ]
// 传入空字符串 : 逐位分割
var test18 = tit.split('');
console.log(test18);
//['h', 'e', 'l', 'l',
	'o', ' ', 'a', 'b',
  	'c', ' ', 'w', 'o',
  	'r', 'l', 'd']

toLowerCase( )

说明:把字符串转换为小写

JS
var tit2 = 'hello WORLD';
// toLowerCase()	把字符串转换为小写
console.log(tit2.toLowerCase());//hello world

toUpperCase( )

说明:把字符串转换为大写

JS
// toUpperCase()	把字符串转换为大写
console.log(tit2.toUpperCase());//HELLO WORLD

trim( )

说明:移除字符串首尾空白

JS
var tit3 ='     hello world     ';
// 输出字符
console.log(tit3);//     hello world     
//输出字符长度
console.log(tit3.length);//21
//输出移除首尾空白后的字符
console.log(tit3.trim());//hello world

var test19 = tit3.trim();
// 输出移除首尾空白后的字符长度
console.log(test19.length);//11

replace( )

replace(patt, newStr)

说明:替换与正则表达式匹配的子串

patt 第一个参数是正则表达式 / 原字符

newStr 替换的字符串

JS
//使用指定的字符串newStr替换匹配patt的字符串
var tit4 = 'hello world wo de';
var test20 = tit4.replace('o', '88');
console.log(test20);//hell88 world wo de

//通过正则匹配要替换的字符串
var test21 = tit4.replace(/o/gi, '88');
console.log(test21);// hell88 w88rld w88 de

search( )

检索与正则表达式相匹配的值

match( )

找到一个或多个正则表达式的匹配



这篇关于JavaScript字符串类型属性和方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程