C# 【转贴】中判断字符串是全角还是半角
2008-10-03 17:23
288 查看
原帖:http://topic.csdn.net/t/20040904/11/3339577.html
全角是指用二个字节来表示的一个字符
半角是用一个字节来表示的一个字符
这样的话我们就可以用string.length 和System.text.Encoding.Default.GetByteCount来判断
其中string.length表示字符串的字符数,
System.text.Encoding.Default.GetByteCount表示字符串的字节数。
将全角的变为半角
string s="GBJ1—86";
char[] c=s.ToCharArray();
for (int i=0;i<c.Length;i++)
{
byte[] b=System.Text.Encoding.Unicode.GetBytes(c,i,1);
if (b.Length==2)
{
if (b[1]==255)
{
b[0]=(byte)(b[0]+32);
b[1]=0;
c[i]=System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
}
//半角
string news=new string(c);
把字母,数字由半角转化为全角
public string ChangeStr( string str)
{
char[] c=str.ToCharArray();
for (int i=0;i<c.Length;i++)
{
byte[] b=System.Text.Encoding.Unicode.GetBytes(c,i,1);
if (b.Length==2)
{
if (b[1]==0)
{
b[0]=(byte)(b[0]-32);
b[1]=255;
c[i]=System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
}
//半角
string strNew=new string(c);
return strNew;
}
判断的方法一:
代码测试报告:只能对单个字符进行判断,如果出现"23" 判断结果是半角,忽略了后面的全角,如果需要判断就要遍历证字符串
string s = null;
s = "A";
MessageBox.Show(((s[0] > 255) ? "全角" : "半角") + " ASCII of " + Convert.ToInt32(s[0]).ToString("x").ToUpper());
s = "A";
MessageBox.Show(((s[0] > 255) ? "全角" : "半角") + " ASCII of " + Convert.ToInt32(s[0]).ToString("x").ToUpper());
//中文的Unicode大概是从4E00 到 9FA0,所以上例一个是0x41 一个是0xFF21
判断的方法二:
代码测试报告:只能对单个字符进行判断,如果出现"23" 判断结果是半角,忽略了后面的全角,如果需要判断就要遍历证字符串
if (checkString.Length == Encoding.Default.GetByteCount(checkString))
{
return true;
}
else
{
return false;
}
全角如下:
if (2 * checkString.Length == Encoding.Default.GetByteCount(checkString))
{
return true;
}
else
{
return false;
}
全角是指用二个字节来表示的一个字符
半角是用一个字节来表示的一个字符
这样的话我们就可以用string.length 和System.text.Encoding.Default.GetByteCount来判断
其中string.length表示字符串的字符数,
System.text.Encoding.Default.GetByteCount表示字符串的字节数。
将全角的变为半角
string s="GBJ1—86";
char[] c=s.ToCharArray();
for (int i=0;i<c.Length;i++)
{
byte[] b=System.Text.Encoding.Unicode.GetBytes(c,i,1);
if (b.Length==2)
{
if (b[1]==255)
{
b[0]=(byte)(b[0]+32);
b[1]=0;
c[i]=System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
}
//半角
string news=new string(c);
把字母,数字由半角转化为全角
public string ChangeStr( string str)
{
char[] c=str.ToCharArray();
for (int i=0;i<c.Length;i++)
{
byte[] b=System.Text.Encoding.Unicode.GetBytes(c,i,1);
if (b.Length==2)
{
if (b[1]==0)
{
b[0]=(byte)(b[0]-32);
b[1]=255;
c[i]=System.Text.Encoding.Unicode.GetChars(b)[0];
}
}
}
//半角
string strNew=new string(c);
return strNew;
}
判断的方法一:
代码测试报告:只能对单个字符进行判断,如果出现"23" 判断结果是半角,忽略了后面的全角,如果需要判断就要遍历证字符串
string s = null;
s = "A";
MessageBox.Show(((s[0] > 255) ? "全角" : "半角") + " ASCII of " + Convert.ToInt32(s[0]).ToString("x").ToUpper());
s = "A";
MessageBox.Show(((s[0] > 255) ? "全角" : "半角") + " ASCII of " + Convert.ToInt32(s[0]).ToString("x").ToUpper());
//中文的Unicode大概是从4E00 到 9FA0,所以上例一个是0x41 一个是0xFF21
判断的方法二:
代码测试报告:只能对单个字符进行判断,如果出现"23" 判断结果是半角,忽略了后面的全角,如果需要判断就要遍历证字符串
if (checkString.Length == Encoding.Default.GetByteCount(checkString))
{
return true;
}
else
{
return false;
}
全角如下:
if (2 * checkString.Length == Encoding.Default.GetByteCount(checkString))
{
return true;
}
else
{
return false;
}
相关文章推荐
- 【转贴】 C#中判断字符串是全角还是半角
- C#中判断字符串是全角还是半角的实现代码
- C#中判断字符串是全角还是半角
- 如何判断C#字符串是全角还是半角
- C# 判断字符串是全角或半角
- 类库------对一些字符串进行操作的类1.判断输入是否数字2.截取字符串函数3.过滤输入信息4.生成随机数5.生成验证码图片6.获取汉字第一个拼音7.半角转全角8.全角转半角
- C#中怎样判断一个字符串中得字符是汉字还是字母
- iOS 判断字符是全角还是半角
- 字符串操作(人民币转成大写/全角字符转半角字符/去掉字符串中重复的子字符串/过滤常见特殊字符/反过滤特殊字符/判断是不是合法手机/字符串匹配的算法)
- C# 全角和半角判断
- 如何判断在javascript中输入的文字是全角还是半角?
- C# 字符串全角 半角转换
- 谈谈c#中 全角 半角 汉字 大小写字母的宽度 及中英文混合字符串对齐的方法
- 用javascript判断字符是全角还是半角
- C#全角半角的判断及转换
- C# 全角和半角转换以及判断的简单代码
- C# 字符串全角 半角转换
- 判断全角半角混合字符串的字节数是否超过指定的字节数。(半角被认为是一个字节)
- 判断字符串是否有汉字、全角半角的判断
- 把字符串中的汉字去掉,然后判断全角和半角