user2504767
user2504767

Reputation:

How to cut a keyword in a String (Java)?

I have the following string in 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*")

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

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

}

Upvotes: 1

Views: 273

Answers (7)

christopher
christopher

Reputation: 27346

Time for a little pseudocode me thinks.

Step 1

You want to split the String up into tokens. You can do this using 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.

Step 2

Parse your data, removing "GOOF" as you see it. You can use the String.replaceAll() for that.

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. This is called a metalanguage, and it's designed to analyse and manipulate Strings. What you want to do, is not take case into account, and you can achieve this using the ?i operator.

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.

Upvotes: -1

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

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

Or, if you need to remove all the versions of 'Goof' without case matching then check this :

    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

Upvotes: 2

Ilya Gazman
Ilya Gazman

Reputation: 32221

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());

Upvotes: 0

Sinto
Sinto

Reputation: 930

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(""));

Upvotes: 0

StoopidDonut
StoopidDonut

Reputation: 8617

You would want to use replaceAll with regex ignoring case. You can use Pattern.quote() for this purpose:

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

Here, it doesn't matter how 'gOOf' is written it would replaceAll occurrences using the regex.

Upvotes: 0

You can use a function replaceAll(String regex, String replacement)

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

Upvotes: 0

Dan D.
Dan D.

Reputation: 32391

Something like this may help you. Replace your if statement with the one below.

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.

Upvotes: 0

Related Questions