如何使用 ReactJS 删除最后 3 个字符的 select 文本?

How do I select text with 3 last characters removed with ReactJS?

我需要在网页上显示文本,但需要剪切最后 3 个字符: Python%22 变为 --> Python

我试过 substring() 但它似乎不能正常工作。 帮助将不胜感激!

render() {
    const { data } = this.state
    return (
      <div className="MainData">
        <div id="dataRows">
          {
            data.map(obj => {
              return (
                <div key={obj.g_prof_url}>
                  **
                  // Here I need to "print" Python not Python%22
                  <p>{obj.g_prof_name.substring(0,-3)}</p>
                  **
                </div>
              )
            })
          }
        </div>
      </div>
    );
  }

您可以使用适用于字符串的 .slice 方法,例如:

obj.g_prof_name.slice(0,obj.g_prof_name.length-3) 

此方法将为您获取从索引为 0 的字符到字符串末尾的子字符串 - 3 索引

这应该有效。

obj.g_prof_name.substring(0, obj.g_prof_name.length - 3)

第二个参数必须为正 - 它是要从返回的子字符串中排除的第一个字符的索引。

试试这个 -

<p>{obj.g_prof_name.substring(0, obj.g_prof_name.length - 3)}</p>