ページ 11

文字数をカウントする

Posted: 2013年3月03日(日) 18:54
by 水波形
文字数のByte数は取得出来ますが、
文字数をカウントする方法が無さそうだったので作ってみました。
一応、半角全角を対応させています。
半角のカタカナは、2つで1文字とカウントされるっぽいですが……

GetTextCount関数として定義してます。

戻り値に文字数、
引数にカウントさせるテキストデータ(BytePtr型)
TextNum = GetTextCount(buf)

▽使用例▽
▽これをコピペしてください▽

Re: 文字数をカウントする

Posted: 2017年10月13日(金) 01:49
by HSABP

コード: 全て選択

'全角チェック(Shift_JIS)
Function isZenkaku(c As Byte) As Long
	If (c > &h80 And c < &hA0) Or (c > &hCF And c < &hF0) Then isZenkaku = 1
End Function

'文字数カウント(Shift_JIS)
Function GetTextCount(Text As BytePtr) As Long
	Dim i As Long

	While Text
		If isZenkaku(Text) Then i++'文字が全角文字のとき
		i++
		GetTextCount++
	Wend

End Function

Re: 文字数をカウントする

Posted: 2018年10月08日(月) 12:06
by たかせ
ISCharAlphaNumeric関数からIsDBCSLeadByte関数に変更するだけでよいと思います。
ただし判定方法は <> FALSEとなります。

Re: 文字数をカウントする

Posted: 2018年10月08日(月) 22:38
by HSABP

コード: 全て選択

Declare Function GetTextCount CDECL lib "msvcrt" Alias "_mbslen" (str As *Byte) As Dword
これで良い