-5

I have a string like this:

"hello I am a example example"

I have get the last word of the string like this

 String lasttWord = mystring.substring(mystring.lastIndexOf(" ")+1);

but I can´t split this because the word is repeated

How can I remove the last word only?

4
  • Please share your research and your attempts including a description of how you debugged it and what your own hypothesis of the problem is. Commented Feb 27, 2016 at 15:17
  • Sounds like a lot of work? Alternatively: consider searching for your problem first. stackoverflow.com/questions/8694984/remove-part-of-string Commented Feb 27, 2016 at 15:18
  • In this example there is not a repeated word ;) @JeroenVannevel Commented Feb 27, 2016 at 15:20
  • What exactly do you want to do? Do you want to remove the last word if the same word is before it? Or do you want to do it for the whole String? What would the result be for "example lala example" and "example example lala"? Commented Feb 27, 2016 at 15:23

2 Answers 2

1

Split the string by space and rename the last word. Then using the StringBuilder concatenate them together back to your original String.

String str = "hello I am a example example"
String[] parts = str.split(" ");
parts[parts.length-1] = "moon";
System.out.println(parts[parts.length-1]); 
StringBuilder sb = new StringBuilder();
for (int i=0; i<parts.length; i++) {
   sb.append(parts[i]);
   sb.append(" ");
}
str = sb.toString();
Sign up to request clarification or add additional context in comments.

5 Comments

This does not provide a generalized solution. What if the duplicates are not the last two substrings?
Works for me because the duplicates are in the last two substrings, thank you so much @Nichar
@user3733523 It does not even compile! What is StringBuffer()?
@user2004685 If you see a typo or mistake, feel free to edit instead of complaining how it won't compile. Quite childish behaviour honestly said. He is satisfied with solution so what's your problem man?
the issue with this is that if the string is "hello I am on the moon moon" then it won't solve the problem
0

If you are looking to remove the duplicates from your String and not sure of the positions of the duplicates occurrences then you can try using both a list (to maintain order) and a set (to remove duplicates).

You can then rebuilt your String using the list without duplicates.

Here is the code snippet:

public static void main (String[] args)
{
    String str = "hello I am a example example"; 
    String[] tokens = str.split(" ");
    Set<String> set = new HashSet<>();
    List<String> list = new ArrayList<>();
    for(String token : tokens) {
        if(!set.contains(token)) {
            set.add(token);
            list.add(token);
        }
    }

    /* Print String */
    for(int i = 0; i < list.size(); i++)
        System.out.print(list.get(i) + " ");
}

Output:

hello I am a example

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.