C#使用正则表达式替换或去掉半角标点符号
C#使用正则表达式替换或去掉半角标点符号
全角转半角:
C# Code:
/// <summary>
/// 使用正则表达式替换或去掉半角标点符号
/// </summary>
/// <param name="keyText"></param>
/// <returns></returns>
private static string RemoveSymbol(string keyText)
{
string pattern = @"[~!@#\$%\^&\*\(\)\+=\|\\\}\]\{\[:;<,>\?\/""]+";
Regex seperatorReg = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
keyText = seperatorReg.Replace(keyText, " ").Trim();
return keyText;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
/// 使用正则表达式替换或去掉半角标点符号
/// </summary>
/// <param name="keyText"></param>
/// <returns></returns>
private static string RemoveSymbol(string keyText)
{
string pattern = @"[~!@#\$%\^&\*\(\)\+=\|\\\}\]\{\[:;<,>\?\/""]+";
Regex seperatorReg = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
keyText = seperatorReg.Replace(keyText, " ").Trim();
return keyText;
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
全角转半角:
C# Code:
///转半角的函数(DBC case)
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248//
public static string ToDBC(string input)
{
char[] array = input.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
if (array[i] == 12288)
{
array[i] = (char)32;
continue;
}
if (array[i] > 65280 && array[i] < 65375)
{
array[i] = (char)(array[i] - 65248);
}
}
return new string(array);
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248//
public static string ToDBC(string input)
{
char[] array = input.ToCharArray();
for (int i = 0; i < array.Length; i++)
{
if (array[i] == 12288)
{
array[i] = (char)32;
continue;
}
if (array[i] > 65280 && array[i] < 65375)
{
array[i] = (char)(array[i] - 65248);
}
}
return new string(array);
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网