百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

javascript内置对象总结 - String

bigegpt 2024-10-19 02:51 10 浏览

一、属性:

1.1 常用属性:

1.1.1 String.length属性 - 该属性返回字符串中字符编码单元的数量

var x = "Mozilla";
var empty = "";

console.log("Mozilla is " + x.length + " code units long") // 输出:Mozilla is 7 code units long
console.log("The empty string is has a length of " + empty.length) // 输出:The empty string is has a length of 0

二、方法:

2.1 常用方法:

2.1.1 String.concat() - 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回

语法:

str.concat(str2, [, ...strN])

案例:

let hello = 'Hello, '
console.log(hello.concat('Kevin', '. Have a nice day.')) // 输出:Hello, Kevin. Have a nice day.

let greetList = ['Hello', ' ', 'Venkat', '!']
"".concat(...greetList)  // "Hello Venkat!"
"".concat({})    // [object Object]
"".concat([])    // ""
"".concat(null)  // "null"
"".concat(true)  // "true"
"".concat(4, 5)  // "45"

2.1.2 String.includes() - 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false

语法:

str.includes(searchString[, position])

案例:

var str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be'));       // true
console.log(str.includes('question'));    // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1));    // false
console.log(str.includes('TO BE'));       // false

2.1.3 String.indexOf() - 方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1

语法:

str.indexOf(searchValue [, fromIndex])

案例:

var anyString = "Brave new world";

console.log(anyString.indexOf("w")) // 第一个w位置为8
console.log(anyString.lastIndexOf("w")) // 最后一个w位置为10
console.log(anyString.indexOf("new")) // 第一个new位置为6
console.log(anyString.lastIndexOf("new")) // 最后一个new位置为6

2.1.4 String.match() - 方法检索返回一个字符串匹配正则表达式的结果

语法:

str.match(regexp)

案例:

const paragraph = 'The quick brown fox jumps over the lazy dog. It barked.';
const regex = /[A-Z]/g;
const found = paragraph.match(regex);

console.log(found) // 输出:Array ["T", "I"]

2.1.5 String.replace() - 方法返回一个由替换值(replacement)替换部分或所有的模式(pattern)匹配项后的新字符串。模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数

语法:

str.replace(regexp|substr, newSubStr|function)

案例:

const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
console.log(p.replace('dog', 'monkey')) // 输出:The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?

const regex = /Dog/i;
console.log(p.replace(regex, 'ferret')) // 输出:The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?

2.1.6 String.search() - 方法执行正则表达式和 String 对象之间的一个搜索匹配

语法:

str.search(regexp)

案例:

const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
const regex = /[^\w\s]/g;

console.log(paragraph.search(regex)) // 输出:43
console.log(paragraph[paragraph.search(regex)]) // 输出:"."

2.1.7 String.slice() - 方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串

语法:

str.slice(beginIndex[, endIndex])

案例:

const str = 'The quick brown fox jumps over the lazy dog.';

console.log(str.slice(31)) // 输出:the lazy dog.

console.log(str.slice(4, 19)) // 输出:"quick brown fox"

console.log(str.slice(-4)) // 输出:"dog."

console.log(str.slice(-9, -5)) // 输出:"lazy"

2.1.8 String.split() - 方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置

语法:

str.split([separator[, limit]])

案例:

const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');
console.log(words[3]) // 输出:"fox"

const chars = str.split('');
console.log(chars[8]) // 输出:"k"

const strCopy = str.split();
console.log(strCopy) // 输出:Array ["The quick brown fox jumps over the lazy dog."]

2.1.9 String.substr() - 方法返回一个字符串中从指定位置开始到指定字符数的字符

警告: 尽管 String.prototype.substr(…) 没有严格被废弃, 但它被认作是遗留的函数并且可以的话应该避免使用。它并非JavaScript核心语言的一部分,未来将可能会被移除掉。如果可以的话,使用 substring() 替代它

语法:

str.substr(start[, length])

案例:

var str = "abcdefghij";

console.log("(1,2): "    + str.substr(1,2));   // (1,2): bc
console.log("(-3,2): "   + str.substr(-3,2));  // (-3,2): hi
console.log("(-3): "     + str.substr(-3));    // (-3): hij
console.log("(1): "      + str.substr(1));     // (1): bcdefghij
console.log("(-20, 2): " + str.substr(-20,2)); // (-20, 2): ab
console.log("(20, 2): "  + str.substr(20,2));  // (20, 2):

2.1.10 String.substring() - 方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集

语法:

str.substring(indexStart[, indexEnd])

案例:

var anyString = "Mozilla";

// 输出 "Moz"
console.log(anyString.substring(0,3));
console.log(anyString.substring(3,0));
console.log(anyString.substring(3,-3));
console.log(anyString.substring(3,NaN));
console.log(anyString.substring(-2,3));
console.log(anyString.substring(NaN,3));

// 输出 "lla"
console.log(anyString.substring(4,7));
console.log(anyString.substring(7,4));

// 输出 ""
console.log(anyString.substring(4,4));

// 输出 "Mozill"
console.log(anyString.substring(0,6));

// 输出 "Mozilla"
console.log(anyString.substring(0,7));
console.log(anyString.substring(0,10));

2.1.11 String.toLocaleLowerCase() - 方法根据任何指定区域语言环境设置的大小写映射,返回调用字符串被转换为小写的格式

语法:

str.toLocaleLowerCase()
str.toLocaleLowerCase(locale)
str.toLocaleLowerCase([locale, locale, ...])

案例:

'ALPHABET'.toLocaleLowerCase(); // 'alphabet'

'\u0130'.toLocaleLowerCase('tr') === 'i';    // true
'\u0130'.toLocaleLowerCase('en-US') === 'i'; // false

let locales = ['tr', 'TR', 'tr-TR', 'tr-u-co-search', 'tr-x-turkish'];
'\u0130'.toLocaleLowerCase(locales) === 'i'; // true

2.1.12 String.toLocaleUpperCase() - 方法根据本地主机语言环境把字符串转换为大写格式,并返回转换后的字符串

语法:

str.toLocaleUpperCase()
str.toLocaleUpperCase(locale)
str.toLocaleUpperCase([locale, locale, ...])

案例:

'alphabet'.toLocaleUpperCase(); // 'ALPHABET'

'Ges??'.toLocaleUpperCase(); // 'GES?SS'

'i\u0307'.toLocaleUpperCase('lt-LT'); // 'I'

let locales = ['lt', 'LT', 'lt-LT', 'lt-u-co-phonebk', 'lt-x-lietuva'];
'i\u0307'.toLocaleUpperCase(locales); // 'I'

2.1.13 String.toLowerCase() - 会将调用该方法的字符串值转为小写形式,并返回

语法:

str.toLowerCase()

案例:

console.log('中文简体 zh-CN || zh-Hans'.toLowerCase()) // 输出:中文简体 zh-cn || zh-hans
console.log( "ALPHABET".toLowerCase() ) // 输出:alphabet

2.1.14 String.toUpperCase() - 方法将调用该方法的字符串转为大写形式并返回(如果调用该方法的值不是字符串类型会被强制转换)

语法:

str.toUpperCase()

案例:

const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toUpperCase()) // 输出:THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

2.1.15 String.toString() - 方法返回指定对象的字符串形式

语法:

str.toString()

案例:

var x = new String("Hello world");
console.log(x.toString()) // 输出:"Hello world"

2.1.16 String.trim() - 方法会从一个字符串的两端删除空白字符。在这个上下文中的空白字符是所有的空白字符

语法:

str.trim()

案例:

const greeting = '   Hello world!   ';

console.log(greeting) // 输出: "   Hello world!   "
console.log(greeting.trim()) // 输出:"Hello world!"

相关推荐

当Frida来“敲”门(frida是什么)

0x1渗透测试瓶颈目前,碰到越来越多的大客户都会将核心资产业务集中在统一的APP上,或者对自己比较重要的APP,如自己的主业务,办公APP进行加壳,流量加密,投入了很多精力在移动端的防护上。而现在挖...

服务端性能测试实战3-性能测试脚本开发

前言在前面的两篇文章中,我们分别介绍了性能测试的理论知识以及性能测试计划制定,本篇文章将重点介绍性能测试脚本开发。脚本开发将分为两个阶段:阶段一:了解各个接口的入参、出参,使用Python代码模拟前端...

Springboot整合Apache Ftpserver拓展功能及业务讲解(三)

今日分享每天分享技术实战干货,技术在于积累和收藏,希望可以帮助到您,同时也希望获得您的支持和关注。架构开源地址:https://gitee.com/msxyspringboot整合Ftpserver参...

Linux和Windows下:Python Crypto模块安装方式区别

一、Linux环境下:fromCrypto.SignatureimportPKCS1_v1_5如果导包报错:ImportError:Nomodulenamed'Crypt...

Python 3 加密简介(python des加密解密)

Python3的标准库中是没多少用来解决加密的,不过却有用于处理哈希的库。在这里我们会对其进行一个简单的介绍,但重点会放在两个第三方的软件包:PyCrypto和cryptography上,我...

怎样从零开始编译一个魔兽世界开源服务端Windows

第二章:编译和安装我是艾西,上期我们讲述到编译一个魔兽世界开源服务端环境准备,那么今天跟大家聊聊怎么编译和安装我们直接进入正题(上一章没有看到的小伙伴可以点我主页查看)编译服务端:在D盘新建一个文件夹...

附1-Conda部署安装及基本使用(conda安装教程)

Windows环境安装安装介质下载下载地址:https://www.anaconda.com/products/individual安装Anaconda安装时,选择自定义安装,选择自定义安装路径:配置...

如何配置全世界最小的 MySQL 服务器

配置全世界最小的MySQL服务器——如何在一块IntelEdison为控制板上安装一个MySQL服务器。介绍在我最近的一篇博文中,物联网,消息以及MySQL,我展示了如果Partic...

如何使用Github Action来自动化编译PolarDB-PG数据库

随着PolarDB在国产数据库领域荣膺桂冠并持续获得广泛认可,越来越多的学生和技术爱好者开始关注并涉足这款由阿里巴巴集团倾力打造且性能卓越的关系型云原生数据库。有很多同学想要上手尝试,却卡在了编译数据...

面向NDK开发者的Android 7.0变更(ndk android.mk)

订阅Google官方微信公众号:谷歌开发者。与谷歌一起创造未来!受Android平台其他改进的影响,为了方便加载本机代码,AndroidM和N中的动态链接器对编写整洁且跨平台兼容的本机...

信创改造--人大金仓(Kingbase)数据库安装、备份恢复的问题纪要

问题一:在安装KingbaseES时,安装用户对于安装路径需有“读”、“写”、“执行”的权限。在Linux系统中,需要以非root用户执行安装程序,且该用户要有标准的home目录,您可...

OpenSSH 安全漏洞,修补操作一手掌握

1.漏洞概述近日,国家信息安全漏洞库(CNNVD)收到关于OpenSSH安全漏洞(CNNVD-202407-017、CVE-2024-6387)情况的报送。攻击者可以利用该漏洞在无需认证的情况下,通...

Linux:lsof命令详解(linux lsof命令详解)

介绍欢迎来到这篇博客。在这篇博客中,我们将学习Unix/Linux系统上的lsof命令行工具。命令行工具是您使用CLI(命令行界面)而不是GUI(图形用户界面)运行的程序或工具。lsoflsof代表&...

幻隐说固态第一期:固态硬盘接口类别

前排声明所有信息来源于网络收集,如有错误请评论区指出更正。废话不多说,目前固态硬盘接口按速度由慢到快分有这几类:SATA、mSATA、SATAExpress、PCI-E、m.2、u.2。下面我们来...

新品轰炸 影驰SSD多款产品登Computex

分享泡泡网SSD固态硬盘频道6月6日台北电脑展作为全球第二、亚洲最大的3C/IT产业链专业展,吸引了众多IT厂商和全球各地媒体的热烈关注,全球存储新势力—影驰,也积极参与其中,为广大玩家朋友带来了...