javascript内置对象总结 - String
bigegpt 2024-10-19 02:51 12 浏览
一、属性:
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!"
相关推荐
- Go语言泛型-泛型约束与实践(go1.7泛型)
-
来源:械说在Go语言中,Go泛型-泛型约束与实践部分主要探讨如何定义和使用泛型约束(Constraints),以及如何在实际开发中利用泛型进行更灵活的编程。以下是详细内容:一、什么是泛型约束?**泛型...
- golang总结(golang实战教程)
-
基础部分Go语言有哪些优势?1简单易学:语法简洁,减少了代码的冗余。高效并发:内置强大的goroutine和channel,使并发编程更加高效且易于管理。内存管理:拥有自动垃圾回收机制,减少内...
- Go 官宣:新版 Protobuf API(go pro版本)
-
原文作者:JoeTsai,DamienNeil和HerbieOng原文链接:https://blog.golang.org/a-new-go-api-for-protocol-buffer...
- Golang开发的一些注意事项(一)(golang入门项目)
-
1.channel关闭后读的问题当channel关闭之后再去读取它,虽然不会引发panic,但会直接得到零值,而且ok的值为false。packagemainimport"...
- golang 托盘菜单应用及打开系统默认浏览器
-
之前看到一个应用,用go语言编写,说是某某程序的windows图形化客户端,体验一下发现只是一个托盘,然后托盘菜单的控制面板功能直接打开本地浏览器访问程序启动的webserver网页完成gui相关功...
- golang标准库每日一库之 io/ioutil
-
一、核心函数概览函数作用描述替代方案(Go1.16+)ioutil.ReadFile(filename)一次性读取整个文件内容(返回[]byte)os.ReadFileioutil.WriteFi...
- 文件类型更改器——GoLang 中的 CLI 工具
-
我是如何为一项琐碎的工作任务创建一个简单的工具的,你也可以上周我开始玩GoLang,它是一种由Google制作的类C编译语言,非常轻量和快速,事实上它经常在Techempower的基准测...
- Go (Golang) 中的 Channels 简介(golang channel长度和容量)
-
这篇文章重点介绍Channels(通道)在Go中的工作方式,以及如何在代码中使用它们。在Go中,Channels是一种编程结构,它允许我们在代码的不同部分之间移动数据,通常来自不同的goro...
- Golang引入泛型:Go将Interface「」替换为“Any”
-
现在Go将拥有泛型:Go将Interface{}替换为“Any”,这是一个类型别名:typeany=interface{}这会引入了泛型作好准备,实际上,带有泛型的Go1.18Beta...
- 一文带你看懂Golang最新特性(golang2.0特性)
-
作者:腾讯PCG代码委员会经过十余年的迭代,Go语言逐渐成为云计算时代主流的编程语言。下到云计算基础设施,上到微服务,越来越多的流行产品使用Go语言编写。可见其影响力已经非常强大。一、Go语言发展历史...
- Go 每日一库之 java 转 go 遇到 Apollo?让 agollo 来平滑迁移
-
以下文章来源于GoOfficialBlog,作者GoOfficialBlogIntroductionagollo是Apollo的Golang客户端Apollo(阿波罗)是携程框架部门研...
- Golang使用grpc详解(golang gcc)
-
gRPC是Google开源的一种高性能、跨语言的远程过程调用(RPC)框架,它使用ProtocolBuffers作为序列化工具,支持多种编程语言,如C++,Java,Python,Go等。gR...
- Etcd服务注册与发现封装实现--golang
-
服务注册register.gopackageregisterimport("fmt""time"etcd3"github.com/cor...
- Golang:将日志以Json格式输出到Kafka
-
在上一篇文章中我实现了一个支持Debug、Info、Error等多个级别的日志库,并将日志写到了磁盘文件中,代码比较简单,适合练手。有兴趣的可以通过这个链接前往:https://github.com/...
- 如何从 PHP 过渡到 Golang?(php转golang)
-
我是PHP开发者,转Go两个月了吧,记录一下使用Golang怎么一步步开发新项目。本着有坑填坑,有错改错的宗旨,从零开始,开始学习。因为我司没有专门的Golang大牛,所以我也只能一步步自己去...
- 一周热门
- 最近发表
- 标签列表
-
- mybatiscollection (79)
- mqtt服务器 (88)
- keyerror (78)
- c#map (65)
- xftp6 (83)
- bt搜索 (75)
- c#var (76)
- xcode-select (66)
- mysql授权 (74)
- 下载测试 (70)
- linuxlink (65)
- pythonwget (67)
- androidinclude (65)
- libcrypto.so (74)
- linux安装minio (74)
- ubuntuunzip (67)
- vscode使用技巧 (83)
- secure-file-priv (67)
- vue阻止冒泡 (67)
- jquery跨域 (68)
- php写入文件 (73)
- kafkatools (66)
- mysql导出数据库 (66)
- jquery鼠标移入移出 (71)
- 取小数点后两位的函数 (73)