Javaで正規表現を使用して文字列から母音を削除するにはどうすればよいですか?
単純文字クラス「[]」は、その中の指定されたすべての文字と一致します。次の式は、xyz以外の文字と一致します。
"[xyz]"
同様に、次の式は、指定された入力文字列のすべての母音に一致します。
"([^aeiouAEIOU0-9\\W]+)";
次に、replaceAll()メソッドを使用して、一致した文字を空の文字列「」に置き換えることにより、一致した文字を削除できます。
例1
public class RemovingVowels {
public static void main( String args[] ) {
String input = "Hi welcome to tutorialspoint";
String regex = "[aeiouAEIOU]";
String result = input.replaceAll(regex, "");
System.out.println("Result: "+result);
}
} 出力
Result: H wlcm t ttrlspnt
例2
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main( String args[] ) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter input string: ");
String input = sc.nextLine();
String regex = "[aeiouAEIOU]";
String constants = "";
System.out.println("Input string: \n"+input);
//Creating a pattern object
Pattern pattern = Pattern.compile(regex);
//Matching the compiled pattern in the String
Matcher matcher = pattern.matcher(input);
//Creating an empty string buffer
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
constants = constants+matcher.group();
matcher.appendReplacement(sb, "");
}
matcher.appendTail(sb);
System.out.println("Result: \n"+ sb.toString()+constants );
}
} 出力
Enter input string: this is a sample text Input string: this is a sample text Result: ths s smpl txtiiaaee
-
JavaScriptで文字列から「、」を削除する方法
メイン文字列とサブ文字列が与えられます。私たちの仕事は、これら2つの引数を取り、サブ文字列を含まないバージョンのメイン文字列を返す関数shedString()を作成することです。 例- shedString('12/23/2020', '/'); 文字列を返す必要があります- '12232020' この関数のコードを書いてみましょう- 例 const shedString = (string, separator) => { //we split the string and make it free of
-
Androidのテキストビュー文字列からすべての母音を削除するにはどうすればよいですか?
この例は、Androidのテキストビュー文字列からすべての母音を削除する方法を示しています ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = &quo