2017-03-07 47 views
0

從連字符前的字符中刪除元音,並從連字符後面出現的字符中刪除任何數字從Java中的字符串中刪除元音和數字?

我試過使用這個,但我沒有正確地逃避連字符?
s = replaceAll("[aeiou\-]", "").replaceAll("[-\0-9]", "");

+0

讓我們看看你已經嘗試什麼:) – TheLostMind

+0

爲什麼不只是做多遍,類似:'string.replaceAll(元音 - 前儀表板, 「」).replaceAll(數字-after-dash,「」)'? – nbrooks

+0

https://codescracker.com/java/program/java-program-delete-vowels-from-string.htm –

回答

1
public static void main(String[] args) { 
    final String toReplace = "magic-8ball"; 

    String splitChar = "-"; 
    String[] split = toReplace.split(splitChar); 

    String replacedPart1 = split[0].replaceAll("[aeiouAEIOU]", ""); 
    String replacedPart2 = split[1].replaceAll("[0-9]", ""); 

    System.out.println(replacedPart1 + splitChar + replacedPart2); 
} 
+0

通常最好給解答添加一些解釋,而不是僅僅傾銷一堆代碼。 – Henry

+0

堆棧溢出是一個問答網站,而不是作業寫作服務。請不要讓用戶有理由相信。謝謝。 –

0
String str="we-are-students-worker2-45man"; 
String regex="[aeiouAEIOU]+(?=-)|(?<=-)\\d+"; 
String newStr=str.replaceAll(regex, ""); 
System.out.println(newStr);//w-ar-students-worker2-man 

//try this