Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove a substring from a given String?

Tags:

java

string

People also ask

How do you remove part of a string in Java?

The first and most commonly used method to remove/replace any substring is the replace() method of Java String class. The first parameter is the substring to be replaced, and the second parameter is the new substring to replace the first parameter.

How do you remove a specific substring from a string in C++?

std::string class provides a member function string::erase() to remove some characters from a given position i.e. string& erase (size_t pos = 0, size_t len = npos); string& erase (size_t pos = 0, size_t len = npos); string& erase (size_t pos = 0, size_t len = npos);

How do I remove a specific character from a string?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.


You could easily use String.replace():

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

You can use StringBuffer

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

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

In your example,

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

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.

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", "");

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