【 JavaScript 】字符串方法

在这里插入图片描述

🎈 常用方法

toLowerCase

  • 把字符串转换为小写 返回新的字符串
const str = "HELLO";
console.log(str); // HELLO
console.log(str.toLowerCase()); // hello

toUpperCase

  • 把字符串转换为大写 返回新的字符串
const str = "hello";
console.log(str); // hello
console.log(str.toUpperCase()); // HELLO

charAt

  • 返回指定下标位置的字符
  • 如果下标超出范围 返回空字符
  • 字符下标从0开始
var str = "hello";
console.log(str.charAt(1)); // e

charCodeAt

  • 返回指定下标位置的unicode编码
  • 如果下标超出范围 返回NaN
var str = "hello";
console.log(str.charCodeAt(1)); // e --- 101

indexOf

  • 返回指定字符第一次出现在字符串中的位置
  • 如果没有找到 返回 -1
  • 两个参数
    • ( 需查找的字符 , 从哪个下标开始查找)
    • 默认从下标0开始查找
var str = "hello";
console.log(str.indexOf('o')); // 4

lastIndexOf

  • lastIndexOf ( item , start )
  • 返回指定的字符在字符串最后出现的位置
  • 如果没有找到 返回 -1
  • 两个参数
    • ( 需查找的字符 , 从哪个下标开始查找)
    • 默认从最后一个字符处开始查找
    • item 想要查找的元素(必选)
    • start 设置起始位置(选填)
    • start 需要 + 1 否则就会直接返回start位置的元素
var str = "helloll";
console.log(str.lastIndexOf('l')); // 6

slice

  • slice ( start , end )
  • 截取元素
  • 返回截取的元素
  • 无法截取到end元素
  • 如果不给参数 就代表直接完全复制
  • 负值参数 从后往前
var str = "helloll";
// 不设置参数
console.log(str.slice()); // hello
// 设置参数
console.log(str.slice(1, 2)); // e	
// 负值参数
console.log(str.slice(-3, -1)); // ol

substring

  • substring ( start , end )
  • 截取元素
  • 返回截取的元素
  • 无法截取到end元素
  • 如果不给参数 就代表直接完全复制
  • 不接受负值的参数
const str = "HELLOWORLD";
console.log(str.substring()); //HELLOWORLD
console.log(str.substring(2)); // LLOWORLD
console.log(str.substring(2, 2)); // 空字符串
console.log(str.substring(2, 4)); // LL

substr( start , length)

  • substr ( start , length )
  • 返回从指定下标开始 到指定长度的字符串
  • 如果没有指定长度 就返回下标开始到结尾的所有字符
  • 包含start位置的字符
  • length个数包括start位置的字符
const str = "hello";
console.log(str.substr(1, 3)); // ell

split

  • 把字符串分割成字符串数组。
  • 自己设置分割符分割字符串
const str = "Hello world";
// 空字符作为切割符
console.log(str.split(""));
['H', 'e', 'l', 'l','o', ' ', 'w', 'o','r', 'l', 'd']

const strs = 'h,e,l,l,o,w,o,r,l,d'
// 逗号作为切割符
console.log(strs.split(","));
['h', 'e', 'l', 'l','o', 'w', 'o', 'r','l', 'd']

const strss = 'h,e,l,l,o,w,o,r,l,d'
// 逗号作为切割符 第二个参数设置返回长度/指定要返回的最大数组的元素个数。
console.log(strss.split(",", 4));
[ 'h', 'e', 'l', 'l' ]

includes

  • 用于检查字符串是否包含指定的字符串或字符。
  • 返回 true / false
var str = "hello";
console.log(str.includes('h')); // true
console.log(str.includes('g')); // false

trim

  • 从一个字符串的两端删除空白字符
  • 无法删除单个字符与单个字符之间的空白字符
var str = '    Nice Space    hello';
console.log(str);
console.log(str.trim());
/*
    Nice Space    hello
Nice Space
*/

var strs = 'A B';
console.log(strs);
console.log(strs.trim());
/*
A B
A B
*/

repeat

  • repeat( number )
  • 返回一个新字符串
  • 该字符串包含被连接在一起的指定数量的字符串的副本。
  • 相当于重复了多少遍原字符串。
var string = "Welcome  ";
console.log(string.repeat(2));
// Welcome  Welcome

startsWidth

  • 检查字符串是否以指定的字符串或字符开始。
  • 返回 true / false
var str = "hello"
console.log(str.startsWith('h')); // true
console.log(str.startsWith('o')); // false

endsWith

  • 检查字符串是否以指定的字符串或字符结束。
  • 返回 true / false
var str = "hello"
console.log(str.endsWith('h')); // false
console.log(str.endsWith('o')); // true

concat

  • str.concat(str1,str2,…)
  • 用于连接两个或多个字符串
  • 此方法不改变现有的字符串
  • 返回拼接后的新的字符串。
var str1 = 'hello';
var str2 = ' world '
var str3 = 'Bingo'
console.log(str1.concat(str2, str3));
// hello world Bingo

🐱‍🏍 正则表达式常用

search

  • search( regexp )
  • 用于检索字符串中指定的子字符串,
  • 检索与正则表达式相匹配的子字符串
  • 如果找到,返回与 regexp 相匹配的子串的起始位置,否则返回 -1。
var str = 'hello world';
var regexp = /h/g;
var regexps = /q/g
console.log(str.search(regexp)); // 0
console.log(str.search(regexps)); // -1

replace

  • replace ( 需要替换的字符 , 想要替换成的字符)
  • 用字符替换字符
  • 常用于正则表达式
const str = 'hello';
// 将 o 替换为 e
console.log(str.replace('o', 'e')); // helle

match

  • 常用于正则表达式
  • 需要设置全局 /g
var str = "to be number one";// 全局匹配 to
var reg = /to/g;
var str1 = str.match(reg);
console.log(str1); // [ 'to' ]
console.log(str.match("Hello")); //null
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值