user3130334
user3130334

Reputation: 55

How do you delete a set string from a word (sWord) in Java?

I need help deleting "fe" from sWord in the System.out part of the code. Just need a quick answer on how to do this, its clear that you cant simply subtract it but I don't know how else to do it, thanks for the help. BTW this is not the full code, simply an excerpt.

else if (sWord.substring(sWord.length()-2,sWord.length()).equalsIgnoreCase("fe"))
        {
        System.out.println("The Plural of" + sWord + "is" + **(sWord-("fe"))** + "ves");
        }

Upvotes: 1

Views: 110

Answers (3)

grepit
grepit

Reputation: 22392

How about :

sWord.replace("fe", "");

For more information about replace you can take a look here

Upvotes: 0

user2148736
user2148736

Reputation: 1323

You can use String replace() method:

String newSWord = sWord.replace("fe", "");

If you need some advanced String modification, you can have a look at Apache Commons Lang library - StringUtils replace() method.

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

If it's only at the end of the string (as in your example), you could do

sWord = sWord.substring(0, sWord.length()-2);

If you want to remove "fe" from anywhere in the sWord you could use,

sWord = sWord.replace("fe", "");

Upvotes: 1

Related Questions