首页 > 其他分享 >从 String.prototype.substring 的区间开始

从 String.prototype.substring 的区间开始

时间:2024-04-01 10:34:08浏览次数:22  
标签:slice end String substring start prototype

因为使用 String.prototype.substring(start, end) 或者 Array.prototype.slice(start, end) 的时候偶尔会想不起来这些函数的区间代表的是什么。在这里记录一下。

不同函数的差异

这些区间都是 [start, end),即是包括 start,但是不包括 end(当没有传入 end 时,end 视为数组或者字符串等的长度)。但是这个一般只是在 0 >= start >= end 成立,不同函数对参数处理有一些差别。比如 String.prototype.substring 会把负数视为 0,start > end 的时候会做交换,详细情况请看 substring 和 substr 之间的区别

JavaScript 中获取字符串的子串有好几个方法 substr(已废弃)、substringslice,鉴于很多对象都有 slice 方法,且 slice 的参数处理和区间都是一致的,推荐使用 sliceString.prototype.slice 兼容性也没有问题。

slice 的负数区间

String.prototype.slice 可以看到即使是负数,也是满足 [start, end) 的比如:

const str = "The morning is upon us.";
str.slice(-3); // 'us.'
str.slice(-3, -1); // 'us'
str.slice(0, -1); // 'The morning is upon us'
str.slice(4, -1); // 'morning is upon us'

Array.prototype.slice 也是一样。

为什么这样设计

为什么数组应该从 0 开始 提出 a) 2 ≤ i < 13 的形式比 b)1 < i ≤ 12 等其他形式更优美。在 Mesa 语言的实践中 a) 2 ≤ i < 13 也比其他形式更少犯错,更简单。

问问 ChatGPT

ChatGPT 3.5 是这样回答的:

  1. 方便计算长度:通过结束索引减去起始索引,可以直接得到子字符串的长度。
  2. 避免边界混淆:使用左闭右开区间可以避免边界混淆,因为结束索引指示的是下一个字符的位置,而不是最后一个字符的位置。

其他语言

其他语言都是类似的左闭右开区间,比如 Java String:substringGo slice
可以看到大部分现代语言的 slice 使用都是一致的,当然像 Ruby((1..5) => 1, 2, 3, 4, 5 and (1...5) => 1, 2, 3, 4)或者有些方法还提供了额外的参数或者语法可以让你将 end 包括进去。

参考资料

标签:slice,end,String,substring,start,prototype
From: https://www.cnblogs.com/ende93/p/18107895

相关文章

  • C++String类
    前言大家好,我是jiantaoyab,本篇文章将给大家介绍String类的常用法,和模拟实现String类。String介绍在cplusplus中,对String有着下面的介绍。Thestandardstringclassprovidessupportforsuchobjectswithaninterfacesimilartothatofastandardcontainerofb......
  • codeforces div4 Double Strings
    #include<iostream>#include<algorithm>#include<cstring>#include<map>usingnamespacestd;intT,n;strings[900005];map<string,int>mm;//存放每一个字符串是否出现过intmain(){ cin>>T; while(T--){ mm.clear();//每次清空mm里面的数......
  • E. Nearly Shortest Repeating Substring
    #include<iostream>#include<algorithm>#include<cstring>#include<cmath>usingnamespacestd;intn,m;intmain(){ cin>>n; while(n--) { //strings; cin>>m; strings; cin>>s; intres=m; f......
  • Yii2-助手类(StringHelper)
    Yii2-助手(StringHelper)截断字符串echoStringHelper::truncate('中文截断字符',4);//'中文截断...'字符串转数组StringHelper::explode('thisisstring','');//[0=>'this'1=>'is'2=>'string......
  • js的String对象
    文章目录1.概念2.方法3.检验QQ3.1.传统方式3.2.正则方式4.扑克牌洗牌1.概念用于处理文本(字符串)的对象。2.方法方法作用charAt()返回在指定位置的字符。indexOf()返回首次出现的位置。substr()有两个参数:下标,数目substring()有两个参数:开始,结束,包头不包尾......
  • toLocaleString 将字符串、日期、数字、数组等对象的本地化
    toLocaleString() 是JavaScript中许多对象(包括 Number、Date 和数组)的一个方法。这个方法返回一个字符串,该字符串表示该对象的本地化版本。这通常意味着它会考虑运行代码的环境的语言和地区设置,来生成一个更易读或更符合当地习惯的字符串表示。对于 Number:当对 Number ......
  • [ARC058F] Iroha Loves Strings
    题意给定\(n\)个字符串\(s_1,s_2,...,s_n\)。你需要在其中选择一些字符串,按照顺序拼接。在所有生成的长度为\(k\)的字符串中,选择字典序最小的一个。\(n\le2000,k\le10^4,\sum|s_i|\le10^6\)Sol考虑一个朴素的dp。设\(f_{i,j}\)表示前\(i\)个字......
  • H. Impartial Strings
    H.ImpartialStringsProblem-H-Codeforces抽象场不传题解......
  • JavaScript快速入门笔记之七(String:字符串类型、RegExp:正则表达式)
    JavaScript快速入门笔记之七(String:字符串类型、RegExp:正则表达式)String:字符串类型什么是字符串?底层本质:一串字符组成的只读字符数组包装类型:临时封装原始类型数据,并提供对数据操作方法的对象——类型名和原始类型名相同!StringNumberBoolean何时使用:不必手动创建!......
  • E. Nearly Shortest Repeating Substring
    原题链接题解1.模拟题,注意细节2.时间复杂度\(O(n·sqrt(n))\)code#include<bits/stdc++.h>usingnamespacestd;intn;strings;intcheck(intlen){intflag=0;for(intk=0;k<len;k++){inta[26]={0};for(inti=k;i<n;i+=len)......