Java 将字符串中的null值替换为"&引用;价值
我有一个Java 将字符串中的null值替换为"&引用;价值,java,Java,我有一个字符串,其中包含空值和一些字符串值。我想用”(空白)值替换该空值。 例如,我有一个字符串值 Cap mbsonsl null我希望得到类似于Cap mbsonsl的结果(替换null) 有人能建议我怎么做吗?String.replaceAll可能就是你想要的 String s = "Cap mbsonsl null"; s = s.replaceAll("null", ""); System.out.println(s); 范例 您应该始终确保运行代码是有意义的 if(test.con
字符串
,其中包含空值
和一些字符串
值。我想用”(空白)
值替换该空值。
例如,我有一个字符串值
Cap mbsonsl null我希望得到类似于Cap mbsonsl的结果(替换null)
有人能建议我怎么做吗?String.replaceAll
可能就是你想要的
String s = "Cap mbsonsl null";
s = s.replaceAll("null", "");
System.out.println(s);
范例
您应该始终确保运行代码是有意义的
if(test.contains("null")) {
// Replace the values.
}
额外阅读
- 阅读Java。它会告诉你
String
能做的所有很棒的事情
此代码仅删除空字。
不删除包含空字的字。
见示例:
String withNull = "remove only null not having null contains value like annulling";
String withoutNull = withNull.replaceAll("\\s*\\bnull\\b\\s*", " ");
System.out.println(withoutNull);
输出是:
remove only not having contains value like annulling
那很好。你试过什么吗?我们能看看你的密码吗String.replace
在这里可能非常有用。事实上,我在选择时有两个concat值。有时我会得到Cap metacin 20(示例)。因此20是正确的,但如果有null代替20,则将其替换为“”。在我面前+1s.replace就足够了,而不是s.replaceAll
String withNull = "remove only null not having null contains value like annulling";
String withoutNull = withNull.replaceAll("\\s*\\bnull\\b\\s*", " ");
System.out.println(withoutNull);
remove only not having contains value like annulling