1

I want to remove a string from another string, not all the letters.

example: "hello world my name is john"
removing: "ewo"
result: "hllo rld my name is john"

my program deletes all the letters that are from removing

String text = "hello world my name is john";
    int num = 1;

    for (int i = 0; i < num; i++) {
        String del = ewo;

        String[] delArray = del.split("");

        for (int j = 0; j < delArray.length; j++) {

            text = text.replace(delArray[j], "");
        }
        System.out.println(text);
    }

My program return: "hll rld my nam is jhn", but that's not what I need

4
  • 2
    But... what do you need? The string "ewo" doesn't appear anywhere in "hello world my name is john" Commented Aug 4, 2018 at 11:16
  • Why don&amp;#39;t you try String.valueOf(detail.toString.charAt(position)); to get the value at each position and delete them based on your requirement. Commented Aug 4, 2018 at 11:26
  • maybe my question is wrong but "hEllo WOrld my name is john" --> i want to delete EWO, the letters from the string may not be next to each other Commented Aug 4, 2018 at 11:28
  • @NikolayPavlov Adding expected output would obviously help others. Commented Jun 17, 2020 at 12:29

6 Answers 6

3

Try this

String text = "hello world my name is john";
int num = 1;

for (int i = 0; i < num; i++) {
    String del = ewo;

    String[] delArray = del.split("");

    for (int j = 0; j < delArray.length; j++) {

        text = text.replaceFirst(delArray[j], "");
    }
    System.out.println(text); //output => hll orld my name is john
}
Sign up to request clarification or add additional context in comments.

Comments

2

From your preferred output, I take it you want to replace only the first matching character. Luckily, Java has a method for this.

Replace this line:

text = text.replace(delArray[j], "");

With this one:

text = text.replaceFirst(delArray[j], "");

And it now only removes the first matching character, as desired.

Comments

2

You can use replaceFirst() instead of replace().It will remove the first occurrency matching with your input.

Comments

1
System.out.prinln("hello world my name is john".replace("orld",""));

2 Comments

This code won't even compile, and it also won't do what the OP intends. Replacing orld will target the literal substring orld. But the OP wants to remove each character in orld, one at at time.
@TimBiegeleisen from the question it is not clear what it is supposed to do. Desired output breaks the rule you have mentioned as it results in hllo - o was supposed to be remove right?
1

You can use replaceFirst() or you can use three loops each for removing e, w, and o and use a break statement thereafter.

Comments

1

This might be what you want

public static void main(String[] args) {
    String str1 = "hello world my name is john";
    String str2 = "ewo";

    int currentCharIndex = 0;
    StringBuilder resultBuilder = new StringBuilder();
    for (char c : str1.toCharArray()) {
        if (currentCharIndex >= str2.length() || c != str2.charAt(currentCharIndex)) {
            resultBuilder.append(c);
        } else {
            currentCharIndex++;
        }
    }
    System.out.println(resultBuilder.toString());
}

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.