-2

I need to pre-process a string and remove some words from it. what i am looking for is something like:-

private static final String[] stopWords = { "is", "on", "of", "with"};
String s1 = "Tiger is Mammal with sharp teeth";
System.out.println("s1 = " + s1);  // "Tiger is Mammal with sharp teeth"
s1.remove(stopWords);  //remove should remove 'is' and 'with'  from s1
System.out.println("s1 = " + s1); // "Tiger Mammal sharp teeth"

I am new to programming so please do consider.

Alexis C.
  • 87,500
  • 20
  • 164
  • 172
learner
  • 757
  • 2
  • 14
  • 35

1 Answers1

1

You can do this with a regular expression:

s1 = s1.replaceAll("\\b(is|on|of|with)\\b","");

That will delete the words, but leave the spaces on both sides, so after that you might want to replace the double spaces with singles:

s1 = s1.replace("  "," ");
khelwood
  • 52,115
  • 13
  • 74
  • 94