![](/img/trans.png)
[英]Replace specific characters in a string without using replace method in Java
[英]Replace characters within a string using the java replace method
我是一名初學者 java 程序員,我在大學課程作業中遇到了一個小問題。
基本上我有一個字符串,我想迭代並用字母“z”替換字母“a”或“e”的所有實例。 例如,如果原始字符串是“hello alan”,那么最終字符串應該是“hzllo zlzn”。
我們需要使用一個包含字符“a”和“e”的字符數組來測試字符串。
我在下面包含了我的代碼,我們還需要使用 charAt() 方法。
public static void main(String[] args) {
String a = ("what's the craic?");
char letters[] = new char[]{'a', 't'};
System.out.println("Before:" + a);
System.out.println("After: " + removeCharacters(a, letters));
}
public static String removeCharacters(String sentence, char[] letters) {
for (int i = 0; i < sentence.length(); i++) {
for (int j = 0; j < letters.length; j++) {
if (sentence.charAt(i) == letters[j]) {
sentence = sentence.replace(sentence.charAt(i), 'z');
} else {
sentence = "No changes nessesary";
}
}
}
return sentence;
}
請幫我解決這個問題。 我不確定我哪里出錯了。 謝謝。
嘗試這個:
public static void main(String[] args) {
String a = "hello alan";
System.out.println("Before:" + a);
System.out.println("After: " + removeCharacters(a));
}
public static String removeCharacters(String sentence) {
if (!sentence.contains("a|e"))
return "No changes necessary";
return sentence.replaceAll("a|e", "z");
}
output 1:
Before:hello alan
After: hzllo zlzn
output 2:
Before:hi world
After: No changes necessary
如果您也被允許使用 replaceAll
"hello alan".replaceAll( "[ae]", "z" ); // hzllo zlzn
與replace
不同的是,在內部使用replaceAll
一個Pattern
,它是從第一個參數[ae]
編譯的,以找到要替換為第二個參數z
的部分。 這個解決方案很簡潔,但是很慢,因為每次調用replaceAll
時都必須編譯Pattern
。
否則使用StringBuilder
char[] letters = new char[] { 'a', 'e' };
StringBuilder buf = new StringBuilder( "hello alan" );
IntStream.range( 0, buf.length() ).forEach( i -> {
for( char c : letters )
if( buf.charAt( i ) == c )
buf.replace( i, i + 1, "z" );
} );
String s = buf.toString(); // hzllo zlzn
與String
不同, StringBuilder
的內容是相互的(意味着您可以更改它)。 所以它只需要創建一次,所有的替換都可以就位。
由於您被迫使用charAt(...)
,一種方法是這樣的:
public static String removeCharacters(String sentence, char[] letters) {
String output = "";
boolean wasChanged = false;
for (int i = 0; i < sentence.length(); i++) {
char ch = sentence.charAt(i);
for (int j = 0; j < letters.length; j++)
if (ch == letters[j]) {
ch = 'z';
wasChanged = true;
break;
}
output += ch;
}
if (wasChanged)
return output;
else
return "No changes necessary";
}
由於String::replace(char oldChar, char newChar)返回一個新字符串,該字符串是用newChar
替換此字符串中所有出現的oldChar
所產生的,因此您不需要嵌套循環來執行此操作。 一種有效的方法如下:
public static String removeCharacters(String sentence, char[] letters) {
String copy = sentence;
for (char letter : letters) {
sentence = sentence.replace(letter, 'z');
}
if (sentence.equals(copy)) {
sentence = "No changes nessesary";
}
return sentence;
}
演示:
public class Main {
public static void main(String[] args) {
// Test
System.out.println(removeCharacters("hello stackoverflow", new char[] { 'a', 'e' }));
System.out.println(removeCharacters("world", new char[] { 'a', 'e' }));
}
public static String removeCharacters(String sentence, char[] letters) {
String copy = sentence;
for (char letter : letters) {
sentence = sentence.replace(letter, 'z');
}
if (sentence.equals(copy)) {
sentence = "No changes nessesary";
}
return sentence;
}
}
Output:
hzllo stzckovzrflow
No changes nessesary
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.