5 个解决方案
#1
如不能保证输入时出错,就全部替换!
曾经的我这样替换过“ , ”
曾经的我这样替换过“ , ”
#2
/// <summary>
/// 全角转换为半角
/// </summary>
/// <param name="sbc">全角字符串</param>
/// <returns>半角字符串</returns>
public static string ConvertSbcToDbc(string sbc)
{
string strReturn = "";
char[] sbcChr = sbc.ToCharArray();
for (int i = 0; i < sbcChr.Length; i++)
{
if (sbcChr[i] == 12288)
{
sbcChr[i] = (char)32;
continue;
}
if (sbcChr[i] > 65280 && sbcChr[i] < 65375)
sbcChr[i] = (char)(sbcChr[i] - 65248);
}
strReturn = new string(sbcChr);
return strReturn;
}
/// <summary>
/// 半角转换为全角
/// </summary>
/// <param name="dbc">半角字符串</param>
/// <returns>全角字符串</returns>
public static string ConvertDbcToSbc(string dbc)
{
string strReturn = "";
char[] dbcChr = dbc.ToCharArray();
for (int i = 0; i < dbcChr.Length; i++)
{
if (dbcChr[i] == 32)
{
dbcChr[i] = (char)12288;
continue;
}
if (dbcChr[i] < 127)
dbcChr[i] = (char)(dbcChr[i] + 65248);
}
strReturn = new string(dbcChr);
return strReturn;
}
#3
/// <summary>
/// 全角转半角的函数(DBC case)
/// </summary>
/// <param name="input">任意字符串</param>
/// <returns>半角字符串</returns>
///<remarks>
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
///</remarks>
public string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
string str = c.ToString().Replace("。", ".");
return new string(c);
}
/// <summary>
/// 判断字符是否为全角字符
/// </summary>
/// <param name="c">任意字符</param>
/// <returns>全角字符返回true,半角返回false</returns>
public bool isCharDBC(char c)
{
if (c > 65280 && c < 65375)
return true;
else
return false;
}
/// <summary>
/// 半角转全角的函数(SBC case)
/// </summary>
/// <param name="input">任意字符串</param>
/// <returns>全角字符串</returns>
///<remarks>
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
///</remarks>
public string ToSBC(string input)
{
//半角转全角:
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 32)
{
c[i] = (char)12288;
continue;
}
if (c[i] < 127)
c[i] = (char)(c[i] + 65248);
}
return new string(c);
}
#4
顶 2 3楼正解
#5
支持一下
#1
如不能保证输入时出错,就全部替换!
曾经的我这样替换过“ , ”
曾经的我这样替换过“ , ”
#2
/// <summary>
/// 全角转换为半角
/// </summary>
/// <param name="sbc">全角字符串</param>
/// <returns>半角字符串</returns>
public static string ConvertSbcToDbc(string sbc)
{
string strReturn = "";
char[] sbcChr = sbc.ToCharArray();
for (int i = 0; i < sbcChr.Length; i++)
{
if (sbcChr[i] == 12288)
{
sbcChr[i] = (char)32;
continue;
}
if (sbcChr[i] > 65280 && sbcChr[i] < 65375)
sbcChr[i] = (char)(sbcChr[i] - 65248);
}
strReturn = new string(sbcChr);
return strReturn;
}
/// <summary>
/// 半角转换为全角
/// </summary>
/// <param name="dbc">半角字符串</param>
/// <returns>全角字符串</returns>
public static string ConvertDbcToSbc(string dbc)
{
string strReturn = "";
char[] dbcChr = dbc.ToCharArray();
for (int i = 0; i < dbcChr.Length; i++)
{
if (dbcChr[i] == 32)
{
dbcChr[i] = (char)12288;
continue;
}
if (dbcChr[i] < 127)
dbcChr[i] = (char)(dbcChr[i] + 65248);
}
strReturn = new string(dbcChr);
return strReturn;
}
#3
/// <summary>
/// 全角转半角的函数(DBC case)
/// </summary>
/// <param name="input">任意字符串</param>
/// <returns>半角字符串</returns>
///<remarks>
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
///</remarks>
public string ToDBC(string input)
{
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 12288)
{
c[i] = (char)32;
continue;
}
if (c[i] > 65280 && c[i] < 65375)
c[i] = (char)(c[i] - 65248);
}
string str = c.ToString().Replace("。", ".");
return new string(c);
}
/// <summary>
/// 判断字符是否为全角字符
/// </summary>
/// <param name="c">任意字符</param>
/// <returns>全角字符返回true,半角返回false</returns>
public bool isCharDBC(char c)
{
if (c > 65280 && c < 65375)
return true;
else
return false;
}
/// <summary>
/// 半角转全角的函数(SBC case)
/// </summary>
/// <param name="input">任意字符串</param>
/// <returns>全角字符串</returns>
///<remarks>
///全角空格为12288,半角空格为32
///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
///</remarks>
public string ToSBC(string input)
{
//半角转全角:
char[] c = input.ToCharArray();
for (int i = 0; i < c.Length; i++)
{
if (c[i] == 32)
{
c[i] = (char)12288;
continue;
}
if (c[i] < 127)
c[i] = (char)(c[i] + 65248);
}
return new string(c);
}
#4
顶 2 3楼正解
#5
支持一下