public static class MyExtensions{
public static bool IsVowel( this char c ){
return new[]{ 'a','e','i','o','u','y','A','E','I','O','U','Y' }.Contains(c);
}
}
那就这样用吧
string test = "Hello how are u";
string result = new string(test.Where( c => !c.IsVowel() ).ToArray()); //result is Hll hw r
//remove vowels in string in C#
string s = "saravanan";
string res = "";
char[] ch = { 'a', 'e', 'i', 'o', 'u' } ;
foreach (char c in ch)
{
for (int i = 0; i < s.Length; i++)
{
if (s[i] == c)
{
res = res + "";
}
else
{
res = res + s[i];
}
}
break;
}
Console.WriteLine(res);
Console.ReadLine();
static string RemoveVowel(string input)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
switch (input[i])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
sb.Append("");
break;
default:
sb.Append(input[i]);
break;
}
}
return sb.ToString();
}
static void Main()
{
//using HashSet
//ExceptWith removes the specified elements from the source set. Here, we strip all
//vowels from the set:
var letters = new HashSet<char>("Mark");
letters.ExceptWith("aeiou");
foreach (char c in letters) Console.Write(c); // Mrk
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btndeletevowel_Click(object sender, EventArgs e)
{
string s1;
string s2;
s1 = textBox1.Text;
s2 = System.Text.RegularExpressions.Regex.Replace(s1, "[aeiouAEIOU]", "");
MessageBox.Show(s2);
}
}
}
9条答案
按热度按时间qni6mghb1#
只需要去掉所有的元音字母(大写字母也一样),然后重新赋给这个名字:
mwg9r5ms2#
我知道这是一个较老的线程,但这里有一个稍微干净/更健壮的方法来完成这一点与正则表达式。
qgzx9mmu3#
其他人可能会提供一个正则表达式的例子,但我会考虑一个直接的方法:
esyap4oy4#
首先创建一个扩展方法来标识元音,您可以在任何需要的地方重用该方法:
那就这样用吧
2nbm6dog5#
kzipqqlq6#
lztngnrs7#
xiozqbni8#
在文本框和正则表达式中使用字符串:使用系统;
3qpi33ja9#