已解决:反应如何从字符串中删除逗号

最后更新: 09/11/2023

与从字符串中删除逗号相关的主要问题是它可能导致不正确的数据。 例如,如果您删除“John, Paul, George”中的逗号,则字符串将变为“John, Paul, George”。 当试图将字符串输入数据库或另一个程序时,这可能会导致问题。

There are a few ways to remove commas from a string in React. One way is to use the .replace() method:

var str = "1,2,3,4,5"; str = str.replace(/,/g, ""); // "12345"

第一行声明了一个名为 str 的变量,并为其分配了字符串“1,2,3,4,5”。 第二行调用 str 上的 .replace() 方法。 .replace() 方法有两个参数:第一个是与要替换的字符匹配的正则表达式,第二个是替换字符。 在这种情况下,我们将所有逗号 (,) 替换为空(“”)。 所以“1,2,3,4,5”变成“12345”。

使用 sith 字符串的命令

ReactDOM.render(, document.getElementById(“root”)); // 渲染 App 组件
ReactDOM.render(, document.getElementById(“app”)); // 在新的 DOM 元素中渲染 App 组件

字符串类型

React 中有几种不同类型的字符串。

ReactDOM.render(您好,世界!, document.getElementById('根')); // => 您好,世界!

这将呈现字符串“Hello, world!” 到 DOM。

您还可以使用字符串文字:
ReactDOM.render(这是一个字符串文字。, document.getElementById('根')); // => 这是一个字符串文字。

相关文章: