轉換全半形字元的函式

2021-04-12 16:36:43 字數 2215 閱讀 5557

vb

public

function

tosbc(

byval

input

asstring

) as

string

'轉全形

dimc

aschar

() =

input

.tochararray

dimi

asinteger=0

while

i <

c.length

ifc(i)

=chrw(32

) then

c(i)

=chrw

(12288

)elseif

ascw(c(i))

<

127then

c(i)

=chrw

((ascw(c(i))

+65248

))end

ifsystem.math.min(system.threading.interlocked.increment(i), i -1

)end

while

return

newstring

(c)end function

public

function

todbc(

byval

input

asstring

) as

string

'轉半形

dimc

aschar

() =

input

.tochararray

dimi

asinteger=0

while

i <

c.length

ifascw(c(i))

=12288

then

c(i)

=chrw(32

)elseif

ascw(c(i))

>

65280

andalso

ascw(c(i))

<

65375

then

c(i)

=chrw

(ascw(c(i))

-65248

)end

ifsystem.math.min(system.threading.interlocked.increment(i), i -1

)end

while

return

newstring

(c)end function

c#/**/

//////

轉全形的函式(sbc case)

//////

任意字串

///全形字串

//////

全形空格為12288,半形空格為32

///其他字元半形(33-126)與全形(65281-65374)的對應關係是:均相差65248

///public

string

tosbc(

string

input)

if(c[i]

<

127)

c[i]=(

char

)(c[i]

+65248);}

return

newstring

(c);               

}/**/

//////

轉半形的函式(dbc case)

//////

任意字串

///半形字串

//////

全形空格為12288,半形空格為32

///其他字元半形(33-126)與全形(65281-65374)的對應關係是:均相差65248

///public

string

todbc(

string

input)

if(c[i]

>

65280

&&c[i]

<

65375

)c[i]=(

char

)(c[i]

-65248

);}   

return

newstring

(c);

}

全半形字元轉換

使用者輸入的內容經常既有全形字符,又有半形字元 在pinyin4j的分詞中,會將全形字符轉換為半形字元,造成分詞前後字元不一致 為了解決這個問題,查詢了unicode表,寫了全半形轉換的工具如下 提供對字串的全形 半形,半形 全形轉換 public class bcconvertutil strin...

全半形轉換

第一種方法 轉全形的函式 sbc case 任意字串 全形字串 全形空格為12288,半形空格為32 其他字元半形 33 126 與全形 65281 65374 的對應關係是 均相差65248 public string tosbc string input if c i 127 c i char ...

全半形轉換

string c x byte b c.getbytes unicode system.out.println arrays.tostring b string d byte g d.getbytes unicode system.out.println arrays.tostring g 結果 2...