プロが教えるわが家の防犯対策術!

VBSでテキストファイルを1行ずつ読み、ある桁からある項目A(20桁)が始まるとします。
項目Aは漢字が入ります。後ろ空白(1バイト空白)詰めです。
その項目Aの桁数を調べるにはどのようにVBSを組めばよいのでしょうか?

VBSでMid関数とかLen関数を使ってもうまく計算してくれてないような感じなのです。
例えば
桁 →123456789012345678901234567890
data1→aaaaaaaaaこんばんわ
data2→aaaaaaaaaおつかれ

strRec1 = Mid(strRecord,10,20) 
とした場合、stRec1には項目Aが入る予定

data1なら5、data2なら4と返ってきてほしいのです。
ちなみにMidBでもそれぞれ10,8で返ってきてもいいです。

言い換えると、桁n~桁mまでの項目に何桁の漢字があるかを調べたいです。

よろしくお願いします。

A 回答 (3件)

SJIS(ASCII)としてカウント(半角1バイト、全角2バイト)で


文字列を取りたいということでしょうか?

上記の例でいくと、「data1→aaaaaaaaaこんばんわ 」の長さは
20というバイト数(桁数ではありません)が帰ってくれば
OKということですよね?(Lenだと15が帰ってくるから)

もし、そうであれば、下記のようなバイト数を取得する
コードを組まなければなりません。
(半角=1バイト、全角=2バイト)

理由:Lenは桁数を読み込んでしまうので、バイトはカウントしません。また、LenBという関数があるんですが、UNICODEでの計算になるので、1文字=2バイトになってしまいます。

■Lenの場合
dim data1
data1 = "aaaaaaaaaこんばんわ"
msgbox CountLen(data1)
function CountLen(byval data)
  dim i
  dim c
  dim counter
  counter = 0
  for i = 1 To Len(data)
    c = asc(mid(data, i, 1))
    if c >= &H00 and c <= &H7E then
      counter = counter + 1
    else
      counter = counter + 2
    end if
  next
  CountLen = counter
end function

■Midの場合

dim data2
data2 = "aaaaaaaaaおつかれ"
msgbox OriginalMid(data2, 9, 3)

function OriginalMid(byval data, byval startIndex, byval length)
  dim i
  dim c
  dim counter
  dim returnValue
  counter = 0
  returnValue = ""
  for i = 1 To Len(data)
    c = asc(mid(data, i, 1))
    if c >= &H00 and c <= &H7E then
      counter = counter + 1
    else
      counter = counter + 2
    end if
    if (counter >= startIndex) and (counter <= startIndex + length - 1) then
      returnValue = returnValue & chr(c)
    end if
    if counter >= startIndex + length then
      OriginalMid = returnValue
      exit function
    end if
  next
end function
    • good
    • 0

私が簡単な例でやるとおかしくないように思うのですが。


メモ帳に下記作成。拡張子.VBSで保存。
Dim fso,f1,objText
Set fso = CreateObject("Scripting.FileSystemObject")
'--
Set f1 = fso.GetFile("C:\Documents and Settings\xxxx\My Documents\text12.txt")
set objText=f1.OpenAsTextStream(1)
Do While objText.AtendOfLine <> True
s=objText.ReadLine
MsgBox s
s1= mid(s,10,len(s)-9)
MsgBox len(s1)
Loop
objText.Close
Set objText=Nothing
Set f1=Nothing
set fso=Nothing
(テキストファイルのあり場所は修正のこと)
ーーー
text12.txt
例データ
123456789東京都大田区
aaaaaaaaa横浜市
eeee12344こんにちは
333334444Clasification
ーー
6,3,5、13と表示されました。
この例やコード内容はおかしいですか。
    • good
    • 0

>テキストファイルを1行ずつ読み、ある桁からある項目A(20桁)…項目Aは漢字が入ります。

後ろ空白(1バイト空白)詰めです。

項目Aが発生するまでは、半角文字列だけですか?
であるなら、項目Aは
strRec1 =Mid(strRecord,10,20)
で正しいです。

漢字桁数は空白(全角や半角)を含まない桁数であれば、
strRec1 =Mid(strRecord,10,20)
LenB(Trim(Replace(strRec1,"□",""))) 'バイト数
Len(Trim(Replace(strRec1,"□",""))) '文字数
です。

さらに、項目Aまでの間に半角と全角が混在するなら
strRec1 = StrConv(MidB(StrConv(strRecord, vbFromUnicode), 10, 20), vbUnicode)
LenB(Trim(Replace(strRec1,"□",""))) 'バイト数
Len(Trim(Replace(strRec1,"□",""))) '文字数
です。

項目Aに半角と全角が混在したバイト数を取得するなら
strRec1 = StrConv(MidB(StrConv(strRecord, vbFromUnicode), 10, 20), vbUnicode)
strRec1 = Trim(Replace(strRec1,"□",""))
LenB(StrConv(strRecord, vbFromUnicode)) 'バイト数
こんな感じで如何ですか?


※ 上記の"□"は全角の空白を示します。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!