【问题标题】:How to delete duplicate from string如何从字符串中删除重复项
【发布时间】:2018-12-04 13:18:25
【问题描述】:

例如如何从字符串中删除重复项

const string = 'PandoraPandora'; to --> Pandora 

const string = 'pandorapandora'; to --> pandora

注意:字符串没有空格。

【问题讨论】:

  • 嗯,这取决于您要支持哪些场景。是否有删除重复的最小字符数?
  • 嗨,欢迎来到 SO。如果您为此编写了无法开始工作的代码,那么您来对地方了。只需edit 问题并将代码的相关部分添加到其中。您需要展示自己的努力,因为 Stack Overflow 不是为我编写代码的服务。另请参阅How to Ask
  • 它总是像“wordword”这样的刺痛还是更复杂?
  • string.slice(0, string.length/2)

标签: javascript


【解决方案1】:

给你:

const str = 'pandorapandora'
const midIndex = Math.floor(str.length/2)
const firstHalf = str.slice(0,midIndex)
const secondHalf = str.slice(midIndex)
const dup = firstHalf == secondHalf ? firstHalf : str
console.log(dup)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    相关资源
    最近更新 更多