回答:
あなたは簡単に使うことができますString.replace():
String helloWorld = "Hello World!";
String hellWrld = helloWorld.replace("o","");
Apache StringUtilsを確認してください。
static String replace(String text, String searchString, String replacement)別の文字列内の文字列のすべての出現を置き換えます。static String replace(String text, String searchString, String replacement, int max)検索文字列の最初の最大値について、文字列をより大きな文字列内の別の文字列に置き換えます。static String replaceChars(String str, char searchChar, char replaceChar)文字列内の文字のすべての出現箇所を別の文字で置き換えます。static String replaceChars(String str, String searchChars, String replaceChars)文字列内の複数の文字を一度に置き換えます。static String replaceEach(String text, String[] searchList, String[] replacementList)別の文字列内のすべての文字列を置き換えます。static String replaceEachRepeatedly(String text, String[] searchList, String[] replacementList)別の文字列内のすべての文字列を置き換えます。static String replaceOnce(String text, String searchString, String replacement)ある文字列を、より大きな文字列内の別の文字列で1回置換します。static String replacePattern(String source, String regex, String replacement)指定された正規表現に一致するソース文字列の各部分文字列を、Pattern.DOTALLオプションを使用して指定された置換文字列に置き換えます。
guava の CharMatcher.removeFrom関数を使用することもできます。
例:
String s = CharMatcher.is('a').removeFrom("bazaar");
Substringは、既存の文字列で置き換えるためにも使用できます。
var str = "abc awwwa";
var Index = str.indexOf('awwwa');
str = str.substring(0, Index);
これは、指定された文字列からすべての部分文字列を削除する実装です
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);
}
isSubstring()メソッドの実装はこちら