【问题标题】:how to reduce a string by introducing dots within specified range如何通过在指定范围内引入点来减少字符串
【发布时间】:2018-06-15 15:12:02
【问题描述】:

我想在指定范围之间引入...,这样我就可以看到文件format

假设我有一个 字符串 sinchan is going to school in india.mp4

假设我想在pos/index 16 到 19 之间介绍...

我的字符串将如下所示: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(可能相差一两个字符)

请帮我提前谢谢!!!

【问题讨论】:

  • 看起来像是一些作业/家庭作业。请研究并尝试自己编写代码,并在遇到代码困难时寻求帮助。
  • 嗨!请使用tour 并通读help center,尤其是How do I ask a good question? 你最好的选择是做你的研究,search 以获取有关 SO 的相关主题,然后试一试。 如果您在进行更多研究和搜索后遇到困难并且无法摆脱困境,请发布您的尝试minimal reproducible example,并具体说明您遇到的问题。人们会很乐意提供帮助。祝你好运!

标签: javascript jquery html node.js


【解决方案1】:
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)

【讨论】:

    【解决方案2】:

    你会想要使用String.indexOf(searchValue)

    一旦您知道正确的起始索引,考虑到您要搜索的单词的长度和结束索引,那么您将需要将字符串分开 String.slice(startIndex, endIndex)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多