0

I want to remove a letter from a String - But only a single occurrence of the letter:

Example: if my word is "aaba" and I want to remove an 'a':

Output would be "aba" - Only the first 'a' is removed. (Not all the 'a's)

I came up with this:

String word = "aaba"
String newWord = word.replace(a, "");

The problem is that newWord='b' instead of 'aba'

Can someone please help? For some reason I am having much difficulty with this seemingly simple problem. What is the best way to solve this problem?

Do I need to create an ArrayList of some sort?

4
  • I'll bet if you look at the String documentation you will find a method that will do exactly what you want. Commented Feb 5, 2015 at 5:18
  • 2
    String#replaceFirst looks interesting... Commented Feb 5, 2015 at 5:19
  • String word = "aaba" String newWord = word.replaceFirst(a, ""); Commented Feb 5, 2015 at 5:29
  • String#replaceFirst uses regex. Not such a good idea. This doesnt work, for example String word = "aa$b$a"; String newWord = word.replaceFirst("$", ""); Commented Feb 5, 2015 at 5:32

4 Answers 4

1

public String replace(char oldChar, char newChar) will replace all occurrences of oldChar in this string with newChar.

You should consider using public String replaceFirst(String regex,String replacement) which replaces the first substring of this string that matches the given regular expression with the given replacement.

String word = "aaba"
String newWord = word.replaceFirst(a, "");
Sign up to request clarification or add additional context in comments.

Comments

0

String replaceFirst() Method

public String replaceFirst(String regex, String replacement)

Parameters:

Here is the detail of parameters:

regex -- the regular expression to which this string is to be matched.

replacement -- the string which would replace found expression.

Code

 public static void main(String[] args) {
        String word = "aaba";
        String newWord = word.replaceFirst("a", "");
        System.out.println(newWord);
    }

Output

aba

Comments

0

you need to first find the index of first occurrence using indexof() metohd, then you can find the text before and after the targeted char by substring() method , at the end you need to use concat() to attach them.

Str.replaceFirst() also works well

Comments

0

You may choose to use public String replaceFirst(String regex, String replacement)

regex -- This is the regular expression to which this string is to be matched.
replacement -- This is the string to be substituted for each match.

Since in your code, you just want to replace first occurence of repeated charecter, you may replace it with blank like below code.

String word = "aaba" 
String newWord = word.replaceFirst(a, "");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.