首页 > 其他分享 >js 中 对 String 的操作

js 中 对 String 的操作

时间:2023-04-23 13:25:13浏览次数:28  
标签:返回 const String js str world 字符串 操作 hello

// charAt():返回指定位置的字符。
const str = "hello";
const char = str.charAt(1); // "e"

// charCodeAt():返回指定位置字符的Unicode编码。
const str = "hello";
const unicode = str.charCodeAt(1); // 101

// concat():连接两个或多个字符串,并返回新的字符串。
const str1 = "hello";
const str2 = "world";
const newStr = str1.concat(" ", str2); // "hello world"

// includes():判断一个字符串是否包含另一个字符串,返回布尔值。
const str = "hello world";
const included = str.includes("world"); // true

// endsWith():判断一个字符串是否以指定的字符结尾,返回布尔值。
const str = "hello world";
const ended = str.endsWith("world"); // true

// indexOf():返回字符串中指定字符的位置,如果未找到则返回-1。
const str = "hello world";
const index = str.indexOf("world"); // 6

// lastIndexOf():返回字符串中指定字符最后出现的位置,如果未找到则返回-1。
const str = "hello world";
const lastIndex = str.lastIndexOf("l"); // 9

// match():在字符串中查找一个正则表达式匹配的内容,返回数组。
const str = "hello world";
const matched = str.match(/o/g); // ["o", "o"]

// repeat():将字符串重复指定的次数,并返回新的字符串。
const str = "hello";
const repeated = str.repeat(3); // "hellohellohello"

// replace():在字符串中用一个新的子串替换某个子串。
const str = "hello world";
const replaced = str.replace("world", "javascript"); // "hello javascript"

// search():搜索字符串中指定的子串,返回匹配到的位置。
const str = "hello world";
const position = str.search(/wo/); // 6

// slice():提取字符串中的一部分,返回新的字符串。
const str = "hello world";
const sliced = str.slice(6, 11); // "world"

// split():将字符串按指定的分隔符拆分成数组。
const str = "hello world";
const splitted = str.split(" "); // ["hello", "world"]

// startsWith():判断一个字符串是否以指定的字符开头,返回布尔值。
const str = "hello world";
const started = str.startsWith("hello"); // true

// substr():从指定位置开始,截取指定长度的字符串。
const str = "hello world";
const substred = str.substr(6, 5); // "world"

// substring():提取字符串中两个指定的索引号之间的字符。
const str = "hello world";
const substr = str.substring(6, 11); // "world"

// toLowerCase():将字符串中的所有字符转换成小写字母。
const str = "HELLO WORLD";
const lowercased = str.toLowerCase(); // "hello world"

// toUpperCase():将字符串中的所有字符转换成大写字母。
const str = "hello world";
const uppercased = str.toUpperCase(); // "HELLO WORLD"

// trim():去除字符串两端的空格,并返回新的字符串。
const str = "   hello world   ";
const trimmed = str.trim(); // "hello world"


// valueOf():返回指定字符串的原始值。
const str = "hello world";
const value = str.valueOf(); // "hello world"

// toString():将任何类型的值转换成字符串。
const num = 123;
const str = num.toString(); // "123"

// localeCompare():比较两个字符串,并返回一个数字表示它们的顺序关系。
const str1 = "apple";
const str2 = "banana";
const comparison = str1.localeCompare(str2); // -1

// padStart():在字符串的开头插入指定数量的字符。
const str = "world";
const padded = str.padStart(10, "hello "); // "hello world"

// padEnd():在字符串的结尾插入指定数量的字符。
const str = "hello";
const padded = str.padEnd(10, " world"); // "hello world"

// toLocaleLowerCase():将字符串中的所有字符转换成小写字母,根据本地化规则。
const str = "HELLO WORLD";
const lowercased = str.toLocaleLowerCase(); // "hello world"

// toLocaleUpperCase():将字符串中的所有字符转换成大写字母,根据本地化规则。
const str = "hello world";
const uppercased = str.toLocaleUpperCase(); // "HELLO WORLD"

//trimStart():去除字符串开头的空格,并返回新的字符串。
const str = "   hello world";
const trimmed = str.trimStart(); // "hello world"

//trimEnd():去除字符串结尾的空格,并返回新的字符串。
const str = "hello world   ";
const trimmed = str.trimEnd(); // "hello world"

//codePointAt():返回在给定位置的字符的Unicode代码点。
const str = "hello world";
const codePoint = str.codePointAt(1); // 101

//fromCharCode():将Unicode编码转换为字符。
const char = String.fromCharCode(97); // "a"

//fromCodePoint():将Unicode代码点转换为字符。
const char = String.fromCodePoint(9731); // "☃"

//normalize():按指定的 Unicode 正规化形式对当前字符串进行编码。
const str = "héllö wõrld";
const normalized = str.normalize(); // "héllö wõrld"

//substrinig():提取字符串中两个指定的索引号之间的字符。
const str = "hello world";
const substring = str.substring(6, 11); // "world"

// match():在字符串中搜索一个指定的值或者一个正则表达式,并返回匹配的结果。
const str = "hello world";
const matched = str.match("world"); // ["world"]

// replace():使用指定的值或者正则表达式替换字符串中的一个子串,并返回新的字符串。
const str = "hello world";
const replaced = str.replace("world", "universe"); // "hello universe"

// search():在字符串中搜索指定的值或者正则表达式,并返回第一个匹配的位置。
const str = "hello world";
const position = str.search("world"); // 6

// slice():提取字符串中的一部分,并返回新的字符串。
const str = "hello world";
const sliced = str.slice(6, 11); // "world"

// split():将字符串拆分成一个数组,通过指定的分隔符来确定每个数组元素。
const str = "hello,world";
const splitted = str.split(","); // ["hello", "world"]

// startsWith():检查字符串是否以指定的值开始。
const str = "hello world";
const startsWithHello = str.startsWith("hello"); // true

// endsWith():检查字符串是否以指定的值结束。
const str = "hello world";
const endsWithWorld = str.endsWith("world"); // true

// toLowerCase():将字符串转换为小写,并返回新的字符串。
const str = "Hello World";
const lowerCase = str.toLowerCase(); // "hello world"

// toUpperCase():将字符串转换为大写,并返回新的字符串。
const str = "Hello World";
const upperCase = str.toUpperCase(); // "HELLO WORLD"

// toString():将一个数值转换为字符串。
const num = 42;
const str = num.toString(); // "42"

// valueOf():返回一个字符串对象的原始值。
const strObj = new String("Hello World");
const value = strObj.valueOf(); // "Hello World"

 

标签:返回,const,String,js,str,world,字符串,操作,hello
From: https://www.cnblogs.com/caitangbutian/p/17346260.html

相关文章

  • windows11 nvm 切换nodejs版本失败
    在windows11,当切换分支命令nvmuse16.15.0显示成功,但用nvmls看的时候发现分支没有切换,可以直接用windows的命令来覆盖原来nodejs文件夹的快捷方式  把这个文件夹删掉,然后用命令重建mklink/dD:\Soft\nvm\nodejsD:\Soft\nvm\nvm\v16.15.0 这样就可以重新指......
  • jsp中display:table根据某列合并其他列
    业务需求是这样的:根据前面的列合并后面的列 代码:<bodyonload="fixRowspan()"><display:tablename="dataList"id="tableList"cellspacing="0"cellpadding="0"requestURI="/x/xx_xxx.action"><di......
  • 快速上手Linux核心命令(三):文件和目录操作命令
    @目录前言cd切换目录pwd显示当前路径ls显示目录下内容及相关属性信息mkdir创建目录tree以树形结构显示目录下的内容touch创建空白文件或改变文件的时间戳属性cp复制文件或目录mv移动或重命名文件rm删除文件或目录chown改变文件或目录的用户用户组chmod改变文件或目录的......
  • js相关的一些知识点
    //"?."是JavaScript中的可选链操作符,用于在对象属性链式调用过程中判断前面的属性是否存在,//如果存在则继续调用,否则返回undefined。这个操作符可以避免因为某个属性不存在而出现程序崩溃或异常情况,提高代码的健壮性和可读性。例如:constperson={name:'张三',age:1......
  • 【Linux】操作系统与进程的概念
    目录冯诺依曼体系注意为什么CPU不直接访问输入或输出设备?跨主机间数据的传递操作系统管理进程描述进程进程的查看和终止 bash通过系统调用创建子进程fork的辨析冯诺依曼体系......
  • nmap工具:一款开源的网络扫描和主机检测工具,可以用于发现计算机系统上运行的端口、服务
    1、nmap是一款开源的网络扫描和主机检测工具,可以用于发现计算机系统上运行的端口、服务以及操作系统等信息。通过nmap的扫描,系统管理员可以获得自己网络环境下的详细情况,包括哪些端口正在监听,哪些服务正在运行等信息,可以在保证网络安全和稳定的前提下优化网络配置,增强网络安全......
  • jackson将java对象转换为json字符串
    1.1. 下载jacksonJackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。相比json-lib框架,Jackson所依赖的jar包较少,简单易用并且性能也要相对高些。而且Jackson社区相对比较活跃,更新速度也比较快。下载地址:http://jackson.codehaus.org/1......
  • 手把手教你进行Scrapy中item类的实例化操作
     接下来我们将在爬虫主体文件中对Item的值进行填充。1、首先在爬虫主体文件中将Item模块导入进来,如下图所示。2、第一步的意思是说将items.py中的ArticleItem类导入到爬虫主体文件中去,将两个文件串联起来,其中items.py的部分内容如下图所示。3、将这个ArticleItem类导入之后,接下来......
  • 王道408操作系统-4.2文件目录 习题总结
    错题复盘第一题散列法一般不用来检索目录,因为想要避免散列冲突就需要大量的存储空间来存放目录,造成不必要的浪费。在树形目录中检索时,应从当前目录开始逐级检索。在上图中,当我想要查找文件N时,使用文件路径/D/p/N查找,很明显分量名P不在D之下,继续往下查找没有任何意义,这时就......
  • 信创操作系统--麒麟Kylin桌面版 (项目三 控制中心-账户类设置与个性化设置)
     账户类设置1 账户设置在安装系统时会创建一个账户,如图1-1所示。图1-1添加用户账户(1)单击【开始】菜单用户头像,弹出用户账户界面,如图1-2所示。图1-2(2)单击【+添加新用户】按钮,即可添加新用户账户,如图1-3所示。图1-3(3)单击【确认】按钮后,弹出授权界面,如图1-4所示。在【密码】输入框中......