我想在一個字每一個元音前插入一個「AB」試圖在字符串中的每個元音前增加一個字符
例如,如果用戶輸入字:火 它必須被更改爲: fabirabe 但我的代碼只是在abfire之前輸入ab。我該如何解決這個問題?
這裏是我到目前爲止的代碼:
import java.util.Scanner;
public class Foothill
{
// class variables shared by more than one method
String prompt;
static String strUserResponse;
// main method
public static void main (String[] args)
{
giveInstructions();
getUserInput();
convertToTurkeyIrish();
vowelCounter();
}
public static String convertToTurkeyIrish()
{
String turkeyIrish = strUserResponse;
String turkeyIrish2;
turkeyIrish2 = "ab" + strUserResponse.replaceAll("(aeiouAEIOU)", "$1ab");
System.out.println("Word In Turkey Irish: " + turkeyIrish2);
return turkeyIrish;
}
public static void vowelCounter()
{
int vowel = 0;
strUserResponse.length();
char vowels;
vowels = ' ';
for (int j = 0; j <= strUserResponse.length() - 1 ; j++)
{
vowels = strUserResponse.charAt(j);
if ((vowels == 'a') || (vowels == 'A') || (vowels == 'e') || (vowel == 'E') || (vowel == 'i')|| (vowels == 'I') || (vowels == 'o') (vowels == 'O') || (vowel == 'u') || (vowels == 'u'))
{
System.out.println("Vowels in " + strUserResponse + ": " + vowel++);
}
}
}
}
這裏沒有捕獲的組,'$ 1'是什麼? – Maroun
@MarounMaroun - 正確!謝謝 – Amit