SBC case half-width conversion class

Source: Internet
Author: User

/* -- Use VB system functions --*/

Using System;
Using System. Collections. Generic;
Using System. Text;

Using Microsoft. VisualBasic;

Namespace CaseConvert
{
/// <Summary>
/// SBC case half-width conversion class (using VB system functions)
/// Defect -- unidentifiable ,. Both of them will be recognized? /Cannot be converted \
/// </Summary>
Class CaseVBConvert
{

/// <Summary>
/// Convert the fullwidth to halfwidth
/// </Summary>
/// <Param name = "sbcCaseStr"> fullwidth string </param>
/// <Returns> halfwidth string </returns>
Public static string Convert_SBC_2_DBC (string sbcCaseStr)
{
Return Strings. StrConv (sbcCaseStr, VbStrConv. Narrow, 0 );
}

/// <Summary>
/// Convert the halfwidth to fullwidth
/// </Summary>
/// <Param name = "dbcCaseStr"> halfwidth string </param>
/// <Returns> fullwidth string </returns>
Public static string Convert_DBC_2_SBC (string dbcCaseStr)
{
Return Strings. StrConv (dbcCaseStr, VbStrConv. Wide, 0 );
}
}
}

/* -- Use C #--*/

Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace CaseConvert
{

/// <Summary>
/// SBC case half-width conversion class (C #)
/// The full-width space is 12288, and the half-width space is 32.
/// The relationship between the half-width (33-126) of other characters and the full-width (65281-65374) is as follows: the difference is 65248.
/// </Summary>
Class CaseCSConvert
{

/// <Summary>
/// Convert the fullwidth to halfwidth
/// </Summary>
/// <Param name = "sbcCaseStr"> fullwidth string </param>
/// <Returns> halfwidth string </returns>
Public static string Convert_SBC_2_DBC (string sbcCaseStr)
{
String strReturn = "";

Char [] sbcChr = sbcCaseStr. 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>
/// Convert the halfwidth to fullwidth
/// </Summary>
/// <Param name = "dbcCaseStr"> halfwidth string </param>
/// <Returns> fullwidth string </returns>
Public static string Convert_DBC_2_SBC (string dbcCaseStr)
{
String strReturn = "";

Char [] dbcChr = dbcCaseStr. 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;
}
}
}

The preceding two common full/halfwidth character conversion functions are collected and sorted.

I personally think the latter one is more appropriate, at least it will not lead to incorrect conversion.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.