ab.com コミュニティ https://www.activebasic.com/forum/ |
|
文字数をカウントする https://www.activebasic.com/forum/viewtopic.php?t=4115 |
ページ 1 / 1 |
作成者: | 水波形 [ 2013年3月03日(日) 18:54 ] |
記事の件名: | 文字数をカウントする |
文字数のByte数は取得出来ますが、 文字数をカウントする方法が無さそうだったので作ってみました。 一応、半角全角を対応させています。 半角のカタカナは、2つで1文字とカウントされるっぽいですが…… GetTextCount関数として定義してます。 戻り値に文字数、 引数にカウントさせるテキストデータ(BytePtr型) TextNum = GetTextCount(buf) ▽使用例▽ [ここをクリックすると内容が表示されます]
Dim Length As Long Dim TextNum As Long Dim buf As *Byte Length = GetWindowTextLength(hEditBox1) buf = malloc(Length+1) GetWindowText(hEditBox1,buf,Length+1) TextNum = GetTextCount(buf) free(buf) ▽これをコピペしてください▽ [ここをクリックすると内容が表示されます]
コード: Function GetTextCount(Text As BytePtr) As Long Dim i As Long Dim Count As Long i=0 Count = 0 Do 'ポインタの最後ならば抜ける If Text = NULL Then Exit Do End If '文字が全角文字のとき If IsCharAlphaNumeric(Text) = FALSE Then If Text[i+1] = NULL Then Count++ Exit Do End If i+=2 Count++ Continue End If Count++ i++ Loop GetTextCount = Count End Function |
作成者: | HSABP [ 2017年10月13日(金) 01:49 ] |
記事の件名: | Re: 文字数をカウントする |
コード: '全角チェック(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 |
作成者: | たかせ [ 2018年10月08日(月) 12:06 ] |
記事の件名: | Re: 文字数をカウントする |
ISCharAlphaNumeric関数からIsDBCSLeadByte関数に変更するだけでよいと思います。 ただし判定方法は <> FALSEとなります。 |
作成者: | HSABP [ 2018年10月08日(月) 22:38 ] |
記事の件名: | Re: 文字数をカウントする |
コード: Declare Function GetTextCount CDECL lib "msvcrt" Alias "_mbslen" (str As *Byte) As Dwordこれで良い |
ページ 1 / 1 | 全ての表示時間は UTC+09:00 です |
Powered by phpBB® Forum Software © phpBB Limited https://www.phpbb.com/ |