-3

Let's say I have an input string of 6 characters followed by a word "hello"

abcdefhello

or another set of data

quernwegngweghello

I mean, the word "hello" will definitely be at the end. How can I remove the word if possible?

The string is dynamic in number, meaning it will change accordingly so I will like to check whether the word contains "hello" in the last 5 characters and remove them if possible.

Rob Audenaerde
  • 19,195
  • 10
  • 76
  • 121
user127886
  • 325
  • 3
  • 7
  • 13
  • 2
    Take a substring... or a regex. I think you answered your own question with the tags. – Andrew Li Nov 05 '16 at 18:53
  • 2
    1. Why is this tagged `jsp`? – ItamarG3 Nov 05 '16 at 18:53
  • 1
    Did you *bother* to read the javadoc of the [`String`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html) class? If you had, you might have found such useful methods as `endsWith()` and `substring()`. Down-vote for lack for research!! – Andreas Nov 05 '16 at 18:57
  • String x="ghhgghello"; if(x.endsWith("hello")) { }...........Remove the word from where ??? – smruti ranjan Nov 05 '16 at 18:59

1 Answers1

1
private static String stripHelloFromEnd(String input) {
  if (input.endsWith("hello")) {
    return input.substring(0, input.length() - "hello".length());
  }
  return input;
}
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135