C# 过滤字符串中的汉字,效率高点的
例如: string content = "13440900984\r\n13440900984\r\n你好\r\n13440900984\r\n你好\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984你好\r\n鸟\r\n13440900984\r\n13440900984\r\n";
有时候字符串可能会比较长,所以求效率比较好的。。
要求过滤之后:
string content = "13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n";
[最优解释]
if(radioButton1.Checked){ control = form2.zhucex;}
else if(radioButton2.Checked){ control =form3.zhuces;}
else if(radioButton3.Checked){ control = warring;}
else if(radioButton4.Checked){ control =suggest;}
else if(radioButton5.Checked){ control =form4.mumawe;}
else if(radioButton6.Checked){ control =drop;}
if (control =="000000")
{
MessageBox.Show("你没有输入任何控制目标!不发控制信号");
richTextBox1.AppendText("你没有输入任何控制目标!不发控制信号");
}
else if(control != "000000")
{
try
{
//记录操作
richTextBox1.AppendText (control + "正在试图控制,等待回应......" + "\r");
stream = client.GetStream();
if(stream.CanWrite )
{
byte[] by = System.Text.Encoding.ASCII.GetBytes(control.ToCharArray ());
stream.Write(by,0,by.Length);
stream.Flush();
threadReceive =new Thread(new ThreadStart(receive));
threadReceive.Start();
}//endif
}//try
catch
{
richTextBox1.AppendText("服务器未连接1控制无效!" +"\r");
MessageBox.Show("服务器未连接1控制无效!" +"\r");
}
}//else if
}
[其他解释]
Regex reg = new Regex(@"[\u4e00-\u9fa5]+");
string content = "13440900984\r\n13440900984\r\n你好\r\n13440900984\r\n你好\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984你好\r\n鸟\r\n13440900984\r\n13440900984\r\n";
string result = reg.Replace(content, "");
richTextBox2.Text = result;
private static void TestChinese()
{
string content = "13440900984\r\n13440900984\r\n你好\r\n13440900984\r\n你好\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984你好\r\n鸟\r\n13440900984\r\n13440900984\r\n";
StringBuilder builder = new StringBuilder(content.Length);
for (int i = 0; i < content.Length; i++)
{
int n = char.ConvertToUtf32(content,i);
if (n < 0x4e00
[其他解释]
n > 0x9fa5)
{
builder.Append(content[i]);
}
}
Console.WriteLine(builder.ToString());
}
static void Main(string[] args)
{
for (int x = 0; x < 20; x++)
{
int tick = Environment.TickCount;
for (int i = 0; i < 10000; i++) TestChineseRegex01();
tick = Environment.TickCount - tick;
int tick1 = Environment.TickCount;
for (int i = 0; i < 10000; i++) TestChineseRegex01();
tick1 = Environment.TickCount - tick1;
Console.WriteLine(tick.ToString() + "," + tick1.ToString());
}
Console.ReadKey();
}
private static void TestChineseRegex01()
{
Regex reg = new Regex(@"[\u4e00-\u9fa5]+");
string content = "13440900984\r\n13440900984\r\n你好\r\n13440900984\r\n你好\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984你好\r\n鸟\r\n13440900984\r\n13440900984\r\n";
string result = reg.Replace(content, "");
//Console.WriteLine(result);
}
private static void TestChinese()
{
string content = "13440900984\r\n13440900984\r\n你好\r\n13440900984\r\n你好\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984你好\r\n鸟\r\n13440900984\r\n13440900984\r\n";
StringBuilder builder = new StringBuilder(content.Length);
for (int i = 0; i < content.Length; i++)
{
int n = char.ConvertToUtf32(content, i);
if (n < 0x4e00
[其他解释]
n > 0x9fa5)
{
builder.Append(content[i]);
}
}
//Console.WriteLine(builder.ToString());
}
static void Main(string[] args)
{
for (int x = 0; x < 20; x++)
{
int tick = Environment.TickCount;
for (int i = 0; i < 10000; i++) TestChineseRegex01();
tick = Environment.TickCount - tick;
int tick1 = Environment.TickCount;
for (int i = 0; i < 10000; i++) TestChineseRegex01();
tick1 = Environment.TickCount - tick1;
int tick2 = Environment.TickCount;
for (int i = 0; i < 10000; i++) TestChineseLinq01();
tick2 = Environment.TickCount - tick2;
Console.WriteLine(tick.ToString() + "\t" + tick1.ToString() + "\t" + tick2.ToString());
}
Console.ReadKey();
}
private static void TestChineseLinq01()
{
string content = "13440900984\r\n13440900984\r\n你好\r\n13440900984\r\n你好\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984你好\r\n鸟\r\n13440900984\r\n13440900984\r\n";
string result = new string(content.Where(c => ((uint)c < 0x4e00
[其他解释]
(uint)c > 0x9fa5)).ToArray());
//Console.WriteLine(result);
}
private static void TestChineseRegex01()
{
Regex reg = new Regex(@"[\u4e00-\u9fa5]+");
string content = "13440900984\r\n13440900984\r\n你好\r\n13440900984\r\n你好\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984你好\r\n鸟\r\n13440900984\r\n13440900984\r\n";
string result = reg.Replace(content, "");
//Console.WriteLine(result);
}
regex foreach linq
203 171 94
156 172 78
171 172 78
171 156 78
172 172 78
171 156 94
156 171 78
156 156 94
156 172 78
156 171 78
172 156 78
171 188 93
156 156 94
156 187 140
156 172 94
156 171 78
172 171 78
156 172 78
172 156 78
171 156 78
void kickoffChinese()
{
string content = "13440900984\r\n13440900984\r\n你好\r\n13440900984\r\n你好\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984你好\r\n鸟\r\n13440900984\r\n13440900984\r\n";
char[] chars = content.ToCharArray();
int offset = 0;
char[] result = new char[chars.Length];
for (int i = 0; i < chars.Length; i++)
{
if (chars[i] < 0x4E00
[其他解释]
论坛眼神最差的算我了,仔细看才发现,居然2次对比的都是regex的。。。。
重贴测试代码
static void Main(string[] args)
{
Console.WriteLine("regex" + "\t\t" + "foreach" + "\t\t" + "linq");
for (int x = 0; x < 20; x++)
{
int tick = Environment.TickCount;
for (int i = 0; i < 10000; i++) TestChineseRegex01();
tick = Environment.TickCount - tick;
int tick1 = Environment.TickCount;
for (int i = 0; i < 10000; i++) TestChineseForeach01();
tick1 = Environment.TickCount - tick1;
int tick2 = Environment.TickCount;
for (int i = 0; i < 10000; i++) TestChineseLinq01();
tick2 = Environment.TickCount - tick2;
Console.WriteLine(tick.ToString() + "\t\t" + tick1.ToString() + "\t\t" + tick2.ToString());
}
Console.ReadKey();
}
private static void TestChineseLinq01()
{
string content = "13440900984\r\n13440900984\r\n你好\r\n13440900984\r\n你好\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984你好\r\n鸟\r\n13440900984\r\n13440900984\r\n";
string result = new string(content.Where(c => ((uint)c < 0x4e00
[其他解释]
chars[i] > 0x9FA5)
{
result[offset] = chars[i];
offset++;
}
}
char[] dest=new char[offset];
Array.Copy(result, dest, offset);
//Console.WriteLine(new string(dest));
}
[其他解释]
n > 0x9fa5)
{
builder.Append(content[i]);
}
}
//Console.WriteLine(builder.ToString());
}
regex foreach linq
203 47 94
171 31 94
156 47 78
171 47 78
156 47 62
172 31 78
156 47 78
156 47 78
156 47 78
156 31 78
218 47 78
172 31 78
171 47 78
156 47 78
172 46 78
156 47 78
172 31 78
156 47 78
171 32 78
171 31 78
REGEX linq kickoffChinese
1563215
1574715
1723116
1564716
1404716
1563116
1564716
1564715
1414716
1404716
1563116
1564716
1404716
1404716
1404716
1564716
1404716
1404716
1414616
1563215
[其他解释]
好吧,按客客说的,我吧regex声明放外面,似乎也改变不大。
static void Main(string[] args)
{
Console.WriteLine("regex\t\tforeach\t\tlinq\t\tkickoffChinese");
for (int x = 0; x < 20; x++)
{
int tick = Environment.TickCount;
for (int i = 0; i < 10000; i++) TestChineseRegex01();
tick = Environment.TickCount - tick;
int tick1 = Environment.TickCount;
for (int i = 0; i < 10000; i++) TestChineseForeach01();
tick1 = Environment.TickCount - tick1;
int tick2 = Environment.TickCount;
for (int i = 0; i < 10000; i++) TestChineseLinq01();
tick2 = Environment.TickCount - tick2;
int tick3 = Environment.TickCount;
for (int i = 0; i < 10000; i++) kickoffChinese();
tick3 = Environment.TickCount - tick3;
Console.WriteLine(tick.ToString() + "\t\t" + tick1.ToString() + "\t\t" + tick2.ToString() + "\t\t" + tick3.ToString());
}
Console.ReadKey();
}
static Regex reg = new Regex(@"[\u4e00-\u9fa5]+", RegexOptions.Compiled);
static string content = "13440900984\r\n13440900984\r\n你好\r\n13440900984\r\n你好\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984你好\r\n鸟\r\n13440900984\r\n13440900984\r\n";
static StringBuilder builder = new StringBuilder(content.Length);
private static void TestChineseLinq01()
{
//string content = "13440900984\r\n13440900984\r\n你好\r\n13440900984\r\n你好\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984\r\n13440900984你好\r\n鸟\r\n13440900984\r\n13440900984\r\n";
string result = new string(content.Where(c => ((uint)c < 0x4e00
[其他解释]
chars[i] > 0x9FA5)
{
result[offset] = chars[i];
offset++;
}
}
char[] dest = new char[offset];
Array.Copy(result, dest, offset);
//Console.WriteLine(new string(dest));
}
regex foreach linq kickoffChinese
140 60 261 40
130 60 170 50
181 70 190 50
120 60 170 41
130 60 170 40
130 60 171 50
120 60 170 40
130 61 170 40
130 60 160 50
121 70 170 50
120 70 241 60
170 70 180 50
151 60 170 50
120 60 171 40
130 60 160 50
130 60 161 50
120 60 170 40
120 61 170 40
130 60 160 50
121 60 170 40