/// 轉全角的函數(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);
}
/// <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);
}
return new string(c);
}
2.
/// 半角轉全角
/// </summary>
/// <param name="BJstr"></param>
/// <returns></returns>
static public string GetQuanJiao(string BJstr)
{
char[] c = BJstr.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;
}
/// <summary>
/// 全角轉半角
/// </summary>
/// <param name="QJstr"></param>
/// <returns></returns>
static public string GetBanJiao(string QJstr)
{
char[] c = QJstr.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 strNew = new string(c);
return strNew;
}
3.使用Microsoft.VisualBasic類庫
在C#項目中添加引用Microsoft.VisualBasic.dll, 可以在C#程序中直接使用VB.NET中豐富的函數 1// 命令行編譯 : csc /r:Microsoft.VisualBasic.dll Test.cs
// 如果是用 Visual Studio .NET IDE, 請按以下方法爲項目添加引用:
// 打開[解決方案資源管理器], 右擊項目名稱, 選擇[添加引用],
// 從列表中選擇 Microsoft Visual Basic .NET Runtime 組件.
class Test
{
static void Main()
{
string s = "博客園-空軍 [skyIV.cnBlogs.com]";
System.Console.WriteLine(s);
s = Strings.StrConv(s, VbStrConv.Wide, 0); // 半角轉全角
s = Strings.StrConv(s, VbStrConv.TraditionalChinese, 0); // 簡體轉繁體
System.Console.WriteLine(s);
s = Strings.StrConv(s, VbStrConv.ProperCase, 0); // 首字母大寫
s = Strings.StrConv(s, VbStrConv.Narrow, 0); // 全角轉半角
s = Strings.StrConv(s, VbStrConv.SimplifiedChinese, 0); // 繁體轉簡體
System.Console.WriteLine(s);
}
}
4.
{
var result = '';
for (i=0 ; i<str.length; i++)
{
code = str.charCodeAt(i);//獲取當前字符的unicode編碼
if (code >= 65281 && code <= 65373)//在這個unicode編碼範圍中的是所有的英文字母已經各種字符
{
result += String.fromCharCode(str.charCodeAt(i) - 65248);//把全角字符的unicode編碼轉換爲對應半角字符的unicode碼
}else if (code == 12288)//空格
{
result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);
}else
{
result += str.charAt(i);
}
}
return result;
}
5.
{
string strBig,to_strBig;
strBig ="ABCDEFGHIJKLMNOPQRSTUVWXYZ01234 '﹃﹄『』$/abcdefghijklmnopqrstuvwxyz.56789*";
to_strBig = "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234 '“”“”$/abcdefghijklmnopqrstuvwxyz.56789*";
int i;
for(i=0;i<strBig.Length;i++)
{
if(direction!="-1")
{
content=content.Replace(strBig[i],to_strBig[i]);
}
else
{
content=content.Replace(to_strBig[i],strBig[i]);
}
}
return content;
}
6.
drop function [dbo].[f_convert]
GO
/*--全角/半角轉換
轉換說明
全角字符從的unicode編碼從65281~65374
半角字符從的unicode編碼從 33~126
空格比較特殊,全角爲 12288,半角爲 32
而且除空格外,全角/半角按unicode編碼排序在順序上是對應的
所以可以直接通過用+-法來處理非空格數據,對空格單獨處理
like的時候,指定排序規則 COLLATE Latin1_General_BIN
是保證字符順序按unicode編碼排序
(此函數部分思路參考了CSDN上大力的轉換函數)
--鄒建 2005.01(引用請保留此信息)--*/
/*--調用示例
declare @s1 varchar(8000)
select @s1='中 2-3456a78STUVabn中國opwxyz'
select dbo.f_convert(@s1,0),dbo.f_convert(@s1,1)
--*/
create function f_convert(
@str nvarchar(4000), --要轉換的字符串
@flag bit --轉換標誌,0轉換成半角,1轉換成全角
)returns nvarchar(4000)
as
begin
declare @pat nvarchar(8),@step int,@i int,@spc int
if @flag=0
select @pat=N'%[!-~ ]%',@step=-65248
else
select @pat=N'%[!-~ ]%',@step=65248
set @i=patindex(@pat COLLATE Latin1_General_BIN,@str)
while @i>0
select @str=stuff(@str,@i,1,nchar(case unicode(substring(@str,@i,1))
when 32 then 12288
when 12288 then 32
else unicode(substring(@str,@i,1))+@step end))
,@i=patindex(@pat COLLATE Latin1_General_BIN,@str)
return(@str)
end
go
--鄒老大書裏的函數
1,2,3,5爲C#的語法,4爲javascript語法,6.爲MS SQL Server語法
例子都是在網上搜索收集到的.