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

javascript内置对象总结 - String

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

一、属性:

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!"

相关推荐

得物可观测平台架构升级:基于GreptimeDB的全新监控体系实践

一、摘要在前端可观测分析场景中,需要实时观测并处理多地、多环境的运行情况,以保障Web应用和移动端的可用性与性能。传统方案往往依赖代理Agent→消息队列→流计算引擎→OLAP存储...

warm-flow新春版:网关直连和流程图重构

本期主要解决了网关直连和流程图重构,可以自此之后可支持各种复杂的网关混合、多网关直连使用。-新增Ruoyi-Vue-Plus优秀开源集成案例更新日志[feat]导入、导出和保存等新增json格式支持...

扣子空间体验报告

在数字化时代,智能工具的应用正不断拓展到我们工作和生活的各个角落。从任务规划到项目执行,再到任务管理,作者深入探讨了这款工具在不同场景下的表现和潜力。通过具体的应用实例,文章展示了扣子空间如何帮助用户...

spider-flow:开源的可视化方式定义爬虫方案

spider-flow简介spider-flow是一个爬虫平台,以可视化推拽方式定义爬取流程,无需代码即可实现一个爬虫服务。spider-flow特性支持css选择器、正则提取支持JSON/XML格式...

solon-flow 你好世界!

solon-flow是一个基础级的流处理引擎(可用于业务规则、决策处理、计算编排、流程审批等......)。提供有“开放式”驱动定制支持,像jdbc有mysql或pgsql等驱动,可...

新一代开源爬虫平台:SpiderFlow

SpiderFlow:新一代爬虫平台,以图形化方式定义爬虫流程,不写代码即可完成爬虫。-精选真开源,释放新价值。概览Spider-Flow是一个开源的、面向所有用户的Web端爬虫构建平台,它使用Ja...

通过 SQL 训练机器学习模型的引擎

关注薪资待遇的同学应该知道,机器学习相关的岗位工资普遍偏高啊。同时随着各种通用机器学习框架的出现,机器学习的门槛也在逐渐降低,训练一个简单的机器学习模型变得不那么难。但是不得不承认对于一些数据相关的工...

鼠须管输入法rime for Mac

鼠须管输入法forMac是一款十分新颖的跨平台输入法软件,全名是中州韵输入法引擎,鼠须管输入法mac版不仅仅是一个输入法,而是一个输入法算法框架。Rime的基础架构十分精良,一套算法支持了拼音、...

Go语言 1.20 版本正式发布:新版详细介绍

Go1.20简介最新的Go版本1.20在Go1.19发布六个月后发布。它的大部分更改都在工具链、运行时和库的实现中。一如既往,该版本保持了Go1的兼容性承诺。我们期望几乎所...

iOS 10平台SpriteKit新特性之Tile Maps(上)

简介苹果公司在WWDC2016大会上向人们展示了一大批新的好东西。其中之一就是SpriteKitTileEditor。这款工具易于上手,而且看起来速度特别快。在本教程中,你将了解关于TileE...

程序员简历例句—范例Java、Python、C++模板

个人简介通用简介:有良好的代码风格,通过添加注释提高代码可读性,注重代码质量,研读过XXX,XXX等多个开源项目源码从而学习增强代码的健壮性与扩展性。具备良好的代码编程习惯及文档编写能力,参与多个高...

Telerik UI for iOS Q3 2015正式发布

近日,TelerikUIforiOS正式发布了Q32015。新版本新增对XCode7、Swift2.0和iOS9的支持,同时还新增了对数轴、不连续的日期时间轴等;改进TKDataPoin...

ios使用ijkplayer+nginx进行视频直播

上两节,我们讲到使用nginx和ngixn的rtmp模块搭建直播的服务器,接着我们讲解了在Android使用ijkplayer来作为我们的视频直播播放器,整个过程中,需要注意的就是ijlplayer编...

IOS技术分享|iOS快速生成开发文档(一)

前言对于开发人员而言,文档的作用不言而喻。文档不仅可以提高软件开发效率,还能便于以后的软件开发、使用和维护。本文主要讲述Objective-C快速生成开发文档工具appledoc。简介apple...

macOS下配置VS Code C++开发环境

本文介绍在苹果macOS操作系统下,配置VisualStudioCode的C/C++开发环境的过程,本环境使用Clang/LLVM编译器和调试器。一、前置条件本文默认前置条件是,您的开发设备已...