代码之家  ›  专栏  ›  技术社区  ›  brooksrelyt Ozal Zarbaliyev

仅当省略号超过指定的字符数时才添加省略号

  •  0
  • brooksrelyt Ozal Zarbaliyev  · 技术社区  · 5 年前

    只有当描述超过指定的字符数时,才可以在描述后添加省略号吗?

    <Markdown
      source={he.decode(`${props.description.substring(0,500)}...`)}
      escapeHtml={false}
    />
    

    每次描述后都会加上“…”

    2 回复  |  直到 5 年前
        1
  •  2
  •   Peter Ambruzs    5 年前

    你应该试试这个。它将添加。。。仅当描述长度大于500时。

    <Markdown
      source={he.decode(`${props.description.substring(0,500)}${props.description.length > 500 ? '...' : ''}`)}
      escapeHtml={false}
    />
    
        2
  •  0
  •   Matt Croak    5 年前

    你在添加 ... 在你表演完 substring 所以不管get字符串是否被截断,你都要连接省略号。

    要传递此信息,可以编写一个快速函数,根据字符长度返回字符串或截断版本。我写这篇文章的时候,为了说明的目的,我只写了5个字符,但是把15个字符改成了500个。

    <Markdown
      source={he.decode(()=>{this.checkChars(props.description)}}
      escapeHtml={false}
    />
    

    checkChars = (str) => {
      var max = 15
      //replace 15 with 500
      return str.length > max ? str.substring(0, max) + '...' : str
    }
    
    console.log(checkChars("HELLO MY NAME IS BUDDY"))