javascript - 如何通过在指定范围内引入点来减少字符串
问题描述
我想...
在指定范围之间引入,以便我可以看到文件format
假设我有一个字符串 sinchan is going to school in india.mp4
假设我想在16 到 19...
之间介绍pos/index
我的字符串将如下所示: sinchan is going...in india.mp4
我的伪代码:
function getFilename(str, startingDot, endingDot, maxLengthRequired) {
str.(introduce `startingDot` till 'endingDot' do not exceed string length more than `maxLengthRequired`)
}
alert(insert("sinchan is going to school in india.mp4", 16, 19,31));
输出:(sinchan is going...in india.mp4
可能相差一两个字符)
请帮助我提前谢谢!
解决方案
let str = 'sinchan is going to school in india.mp4'
function trans (str, start, end, max) {
if (start + 3 > max) return new Error('start add three should not more than max')
let result = str.slice(0, start) + '...' + str.slice(end)
if (result.length <= max) return result
return result.slice(0, start + 3) + result.slice((start + 3 - max))
}
trans(str, 16, 19, 31)
推荐阅读
- java - 有没有办法检查Java中的流是否有限?
- sqlite - 不支持数据库协议“sqlite” - 无法初始化 zdb 连接池()
- c# - 在 Entity Framework Core 2.1 中为特定环境播种数据
- laravel-5 - 如何在 Eloquent 中获得相关模型列的总和
- java - 以编程方式使用 SparkSQL 将 null/undefined/unknown 值替换为最常见的值
- uiimage - 用于馈送 SCNTechnique 的 sampler2D 输入的低成本图像到 NSData 转换
- vba - 将多个范围复制到数组中而不循环
- java - Hibernate Mappings 导致自动创建附加列 - Spring
- r - 如果其他梯子在R中不起作用
- java - Java Firebase Admin SDK - 无法解析 GoogleCredentials