我在 Java 中有以下字符串:
String test = "Goof 23N, 45, 88, GOOF 33*, 12-24";
现在我想从字符串中删除单词“Goof”,并且我想将最初的23N输入保存在一个单独的字符串中(但是如何删除这个关键字并保存最初输入的“23N”或“33*”)
for(String tmpTest : test.split(",")){
if (tmpTest.toLowerCase.contains("goof")){
String separateString = // implementation unclear
}
}
最佳答案
也许你可以尝试一下:
String test = "Goof 23N, 45, 88, GOOF 33*, 12-24";
String value = test.replaceFirst("Goof", "");
Output: 23N, 45, 88, GOOF 33*, 12-24
或者,如果您需要删除所有不区分大小写的“Goof”版本,请检查以下内容:
String test = "Goof 23N, 45, 88, GOOF 33*, 12-24";
// (?i) in the below regex will ignore case while matching
String value = test.replaceAll("(?i)Goof\\s*", "");
Output: 23N, 45, 88, 33*, 12-24
关于java - 如何剪切字符串中的关键字(Java)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21015379/