Belgi
Belgi

Reputation: 15082

How can I remove a substring from a given String?

Is there an easy way to remove substring from a given String in Java?

Example: "Hello World!", removing "o""Hell Wrld!"

Upvotes: 275

Views: 920394

Answers (14)

Tom Carmi
Tom Carmi

Reputation: 604

In addition to @DwB answer, you can also use StringUtils remove:

String hello = "hello world";
String hellYeah = StringUtils.remove(hello, "o");

or removeIgnoreCase:

String hello = "hellO world";
String hellYeah = StringUtils.remove(hello, "o");

Upvotes: 1

sajeewaI-
sajeewaI-

Reputation: 4845

You can use

String helloWorld = "Hello World";
String target = "e";
String replacement = "";
String replacedString = helloWorld.replace(target, replacement);

The answer is = Hllo World

or you can use regex

String original = "Java is one of best languages. OOP can be used in Java";
String regexTarget = "\\bJava\\b";
String replacedWord = original.replaceAll(regexTarget, "Python");

The answer is = Python is one of best languages. OOP can be used in Python

Upvotes: 1

KV Prajapati
KV Prajapati

Reputation: 94653

You should have to look at StringBuilder/StringBuffer which allow you to delete, insert, replace char(s) at specified offset.

Upvotes: 6

Harsh
Harsh

Reputation: 480

If you know the start and end index you may use this

string = string.substring(0, start_index) + string.substring(end_index, string.length());

Upvotes: 4

Debashish Dwivedi
Debashish Dwivedi

Reputation: 367

You can use Substring also for replacing with existing string:

var str = "abc awwwa";
var Index = str.indexOf('awwwa');
str = str.substring(0, Index);

Upvotes: 5

Sean
Sean

Reputation: 3052

private static void replaceChar() {
    String str = "hello world";
    final String[] res = Arrays.stream(str.split(""))
            .filter(s -> !s.equalsIgnoreCase("o"))
            .toArray(String[]::new);
    System.out.println(String.join("", res));
}

In case you have some complicated logic to filter the char, just another way instead of replace().

Upvotes: 1

OneSix
OneSix

Reputation: 71

replaceAll(String regex, String replacement)

Above method will help to get the answer.

String check = "Hello World";
check = check.replaceAll("o","");

Upvotes: 4

Pratik Patil
Pratik Patil

Reputation: 3753

Here is the implementation to delete all the substrings from the given string

public static String deleteAll(String str, String pattern)
{
    for(int index = isSubstring(str, pattern); index != -1; index = isSubstring(str, pattern))
        str = deleteSubstring(str, pattern, index);

    return str;
}

public static String deleteSubstring(String str, String pattern, int index)
{
    int start_index = index;
    int end_index = start_index + pattern.length() - 1;
    int dest_index = 0;
    char[] result = new char[str.length()];


    for(int i = 0; i< str.length() - 1; i++)
        if(i < start_index || i > end_index)
            result[dest_index++] = str.charAt(i);

    return new String(result, 0, dest_index + 1);
}

The implementation of isSubstring() method is here

Upvotes: 1

Md Tariqul Islam
Md Tariqul Islam

Reputation: 2804

This works good for me.

String hi = "Hello World!"
String no_o = hi.replaceAll("o", "");

or you can use

String no_o = hi.replace("o", "");

Upvotes: 8

DwB
DwB

Reputation: 38328

Check out Apache StringUtils:

  • static String replace(String text, String searchString, String replacement) Replaces all occurrences of a String within another String.
  • static String replace(String text, String searchString, String replacement, int max) Replaces a String with another String inside a larger String, for the first max values of the search String.
  • static String replaceChars(String str, char searchChar, char replaceChar) Replaces all occurrences of a character in a String with another.
  • static String replaceChars(String str, String searchChars, String replaceChars) Replaces multiple characters in a String in one go.
  • static String replaceEach(String text, String[] searchList, String[] replacementList) Replaces all occurrences of Strings within another String.
  • static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList) Replaces all occurrences of Strings within another String.
  • static String replaceOnce(String text, String searchString, String replacement) Replaces a String with another String inside a larger String, once.
  • static String replacePattern(String source, String regex, String replacement) Replaces each substring of the source String that matches the given regular expression with the given replacement using the Pattern.DOTALL option.

Upvotes: 10

Magdi
Magdi

Reputation: 119

You can use StringBuffer

StringBuffer text = new StringBuffer("Hello World");
text.replace( StartIndex ,EndIndex ,String);

Upvotes: 11

Emil
Emil

Reputation: 13789

You can also use guava's CharMatcher.removeFrom function.

Example:

 String s = CharMatcher.is('a').removeFrom("bazaar");

Upvotes: 2

Justin Niessner
Justin Niessner

Reputation: 245479

You could easily use String.replace():

String helloWorld = "Hello World!";
String hellWrld = helloWorld.replace("o","");

Upvotes: 528

Cory Kendall
Cory Kendall

Reputation: 7324

replace('regex', 'replacement');
replaceAll('regex', 'replacement');

In your example,

String hi = "Hello World!"
String no_o = hi.replaceAll("o", "");

Upvotes: 8

Related Questions