简体   繁体   English

如何在字符串(Java)中剪切关键字?

[英]How to cut a keyword in a String (Java)?

I have the following string in Java: 我在Java中有以下字符串:

String test = "Goof 23N, 45, 88, GOOF 33*, 12-24";

Now i want to cut the word "Goof" away from the String and i want to save the originally 23N input in a separate string (but how can a delete this keyword and save the originally input "23N" or "33*") 现在,我想从字符串中删除单词“ Goof”,我想将原来的23N输入保存在单独的字符串中(但是如何删除此关键字并保存原来的输入“ 23N”或“ 33 *”)

for(String tmpTest : test.split(",")){

  if (tmpTest.toLowerCase.contains("goof")){
      String separateString = // implementation unclear
  }

}

May be you can try this out: 也许您可以尝试一下:

    String test = "Goof 23N, 45, 88, GOOF 33*, 12-24";
    String value = test.replaceFirst("Goof", "");

Output: 23N, 45, 88, GOOF 33*, 12-24 输出:23N,45,88,GOOF 33 *,12-24

Or, if you need to remove all the versions of 'Goof' without case matching then check this : 或者,如果您需要删除所有版本的'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 输出:23N,45、88、33 *,12-24

Something like this may help you. 这样的事情可能会帮助您。 Replace your if statement with the one below. 将您的if语句替换为下面的语句。

int index = tmpTest.toLowerCase().indexOf("goof");
if (index >= 0) {
  String value = tmpTest.substring(index + 5);
}

Also, be careful with the compile errors you have in your code. 另外,请注意代码中的编译错误。

You can use a function replaceAll(String regex, String replacement) 您可以使用函数replaceAll(String regex, String replacement)

String test = "Goof 23N, 45, 88, GOOF 33*, 12-24";
test = test.replaceAll("Goof","").replaceAll("GOOF","");

You would want to use replaceAll with regex ignoring case . 您可能想在正则表达式中使用replaceAll而忽略case You can use Pattern.quote() for this purpose: 为此,可以使用Pattern.quote()

String keyword = "gOOf";

String test = "Goof 23N, 45, 88, GOOF 33*, 12-24";
String replaceString = "(?i)" + Pattern.quote(keyword);

test = test.replaceAll(replaceString, "");
System.out.println(test);

Output: 输出:

23N, 45, 88, 33*, 12-24 23N,45、88、33 *,12-24

Here, it doesn't matter how 'gOOf' is written it would replaceAll occurrences using the regex. 在这里,'gOOf'的写法无关紧要,它将使用正则表达式替换所有出现的内容。

Just one more way to do it... 只是另一种方法...

        String text = "Goof     23N, 45, 88, GoOF 33*, 12-24";
        //if you want to remove trailing spaces use 'goof\\s*' as regex
        String regex="goof";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);  
        Matcher m = p.matcher(text);        
        System.out.println(m.replaceAll(""));
final String GOOF = "goof";
String input = "Goof 23N, 45, 88, GOOF 33*, 12-24";
String inputLawerCase = input.toLowerCase();
String inputWithoutGoof = inputLawerCase.replaceFirst(GOOF, "");
String output = input.substring(input.length() - inputWithoutGoof.length());

//In case Goof is not at the beginning of the string
int goofIndex = inputLawerCase.indexOf(GOOF);
output = input.substring(0, goofIndex) + input.substring(goofIndex + GOOF.length());

Time for a little pseudocode me thinks. 我认为是时候使用一些伪代码了。

Step 1 第1步

You want to split the String up into tokens . 您想将String拆分为tokens You can do this using String.split() . 您可以使用String.split()进行此操作。

Let input equal "this, is, just, a, GOOFtest".
// It's a comma that separates each token, so let's split on that.
Let tokens equal input.split(",").

NOTE: If you want to preserve your input as a String , then don't do this step. 注意:如果要将输入保留为String ,则不要执行此步骤。

Step 2 第2步

Parse your data, removing "GOOF" as you see it. 解析数据,删除"GOOF" You can use the String.replaceAll() for that. 您可以使用String.replaceAll()

for every token in tokens
    let token equal token.replaceAll("GOOF", nothing).

NOTE : If GOOF can come in different cases, it's time for some regexp . 注意 :如果GOOF可以在不同情况下出现,那么该使用regexp This is called a metalanguage , and it's designed to analyse and manipulate String s. 这称为metalanguage ,旨在分析和处理String What you want to do, is not take case into account, and you can achieve this using the ?i operator. 您要做的是不考虑case ,您可以使用?i运算符来实现。

String regex = "(?i)GOOF";
String parsedInput = input.replaceAll(regex, "");
// And that's the only bit of Java you're getting!!

Step 3 第三步

???? ????

Step 4 第四步

Profit! 利润! You have an array containing only the values, without GOOF appearing. 您有一个仅包含值的数组,而没有出现GOOF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM