←
▼
▲
文字列 + 文字列 = 連結した文字列
文字列 & 文字列 = 連結した文字列
文字列 + 数値 = 「型が一致しません」
文字列 & 数値 = 連結した文字列
文字列 + Nothing = 「オブジェクト型の変数は設定されていません」
文字列 & Nothing = 「オブジェクト型の変数は設定されていません」
文字列 + Null = 代入可能だが echo すると「型が一致しません」
文字列 & Null = 文字列
←
▼
▲
Dim re : Set re = CreateObject("VBScript.RegExp")
re.Pattern = "^aaa.*bbb.c"
WScript.echo re.Test( "aaaxxxbbbyc" )
→ 正規表現
-1
出力
(True)
関連
メソッド
Boolean IgnoreCase
大文字小文字の違いでもマッチするかどうか
デフォルト = False
"a.*z"
サンプル
←
▼
▲
テストされる
文字列
axbmacb
a.b
True
axb, acb
axbmacb
x
False
x
True
True
axbmacb
a.b
False
入力データ
出力データ
axb
True
False
-
False
^x
axbmacb
←
▼
▲
Function RegExp::Execute( s as string ) as Matches
正規表現にマッチするかどうかテストし、マッチしたものを返します。
【引数】
s
テストされる文字列
返り値
マッチしたもの
【補足】
に検索条件を設定してから呼び出してください。
Dim re : Set re = CreateObject("VBScript.RegExp")
Dim matches, match
re.Pattern = "a.b"
re.Global = True
Set matches = re.Execute("raxbmacb")
WScript.echo "Match count = " & matches.Count
For Each match In matches
WScript.echo "FirstIndex = " & match.FirstIndex & _
", Length = " & match.Length & ", Value = " & match.Value
Next
出力
Match count = 2
FirstIndex = 1, Length = 3, Value = axb
FirstIndex = 5, Length = 3, Value = acb
正規表現 = "a.b", テストされる文字列 = "raxbmacb", 複数検索する
サンプル
RegExp::Global = False のときは、Matches::Count は 0 か 1 のどちらかになります。
←
▼
▲
Function RegExp::Test( s as string ) as Boolean
正規表現にマッチするかどうかテストし、1つでもマッチしたかどうかを返します。
【引数】
s
テストされる文字列
【補足】
返り値
True=マッチした文字列があった
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "^aaa.*bbb.c"
WScript.echo re.Test("aaaxxxbbbyc")
-1
出力
(True)
サンプル
に検索条件を設定してから呼び出してください。
(もし 0 なら False)
←
▼
▲
Function RegExp::Replace( s as string, to as string ) as string
正規表現を使って、文字列の一部を置換します。
【引数】
s
to
入力文字列(置換前)
置換後の文字列
返り値
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "a.b"
re.Global = True
WScript.echo re.Replace("raxbmacb", "---")
r---m---
出力
サンプル
出力文字列(置換後)
【補足】
に検索条件を設定してから呼び出してください。
→ GetRegExpFromWildcard (VBSLib)
←
▼
▲
Dim RegExp::MultiLine as boolean
Pattern に、行頭 "^"、行末 "$" が使えるようにします。
Dim re : Set re = CreateObject("VBScript.RegExp")
Dim matches, match
re.Pattern = "A.*"+vbCR+"?"+vbLF+"^D"
re.MultiLine = True
Set matches = re.Execute( "ABC"+ vbCRLF +"DEF"+ vbCRLF )
echo "Match count = " & matches.Count
For Each match In matches
echo "FirstIndex = " & match.FirstIndex & _
", Length = " & match.Length & ", Value = " & match.Value
Next
サンプル
"ABC"+ vbCRLF +"DEF"+ vbCRLF に対して、"A.*"+vbCR+"?"+vbLF+"^D" がマッチするには、
MultiLine を True にする必要があります。 "^" がない "A.*"+vbCR+"?"+vbLF+"D" にマッチ
するには、MultiLine は False でも構いません。
改行文字は、任意の文字 "." や、行頭 "^" や、行末"$" にマッチしないので、 vbCR+"?"+vbLF
を指定する必要があります。 これは、MultiLine の値がどちらでも、違いはありません。
Match count = 1
FirstIndex = 0, Length = 6, Value = ABC
D
出力
←
▼
▲
Integer FirstIndex
マッチした位置、最初=0、2バイト文字も 1 でカウント
Integer Length
マッチした文字列の文字数、2バイト文字も 1 でカウント
String Value
マッチした文字列
サンプル
でテストした結果です。
サンプル
Dim re : Set re = CreateObject("VBScript.RegExp")
Dim matches, match
re.Pattern = "<DIV .*>"
re.Global = True
Set matches = re.Execute( text )
For Each match In matches
attr = sscanf( match.Value, "id=""%s""" )
text = Left( text, match.FirstIndex ) +_
"<DIV id="""+ attr +""" plus=""add"">" +_
Mid( text, match.FirstIndex + match.Length + 1 )
Next
match
任意の id属性(ワイルドカード)を持つ DIVタグに、plus 属性を加える
←
▼
▲
Function CInt( s as string ) as integer
文字列や浮動小数を整数型に変換します。
s
"1"
返り値
1
"A"
(エラー)型が一致しません Err=13
"1+1"
(エラー)型が一致しません Err=13
2
"1.5"
1
"1.4"
(エラー)オーバーフローしました。 Err=6
32768
-32769
(エラー)オーバーフローしました。 Err=6
関連
小数部は四捨五入します。
小数部分を切り捨てる
←
▼
▲
Function Eval( s as string ) as integer
式が書かれた文字列を計算します。
返り値
s
"1"
1
"A"
Empty
"1+1"
2
関連
←
▼
▲
Function FormatNumber( Number as integer [, Decimal as integer, bZero as integer,
bMinus as integer, bKeta as integer ] ) as string
数値を、書式で整列した文字列に変換します。
【引数】
Number
Decimal
変換前の数値
小数の桁数。 四捨五入されます。 省略時 = 2
返り値
数値を、書式で整列した文字列
bZero
整数部が 0 のとき 0 を付けるか。 True=0.1、False=.1、省略時= OS設定(True)
'// デフォルト
FormatNumber( 1 ) = "1.00"
'// 小数点の桁数
FormatNumber( 1, 0 ) = "1"
FormatNumber( 1, 1 ) = "1.0"
FormatNumber( 1, 2 ) = "1.00"
FormatNumber( 1.5, 0 ) = "2"
'// 整数部が 0 のとき
FormatNumber( 0.5, 1, True ) = "0.5"
FormatNumber( 0.5, 1, False ) = ".5"
'// マイナスのとき
FormatNumber( -0.5, 1,, True ) = "(0.5)"
FormatNumber( -0.5, 1,, False ) = "-0.5"
'// 桁を区切るコンマ
FormatNumber( 12345, 0,,, True ) = "12,345"
FormatNumber( 12345, 0,,, False ) = "12345"
サンプル:
bMinus
負の数のとき - 記号ではなく ( ) で囲むかどうか。 省略時=OS設定(False)
bKeta
3桁ごとにコンマで区切るかどうか。 省略時=OS設定(False)
数値の先頭に 0 をつけるときは、下記のようにします。
Right( "00" & 27, 3 ) = "027"
3桁のときは、0 を2つ並べます
サンプル:
FormatNumber( 12345.6, 2,,, False ) = "12345.60"
小数2桁、コンマ区切りなし
←
▼
▲
Function Hex( Num as integer ) as string
数値から、16進数の文字列に変換します。
サンプル:
Hex( 16 ) = "10"
CLng( "&h" & "10" ) = 16
関連
16進数の文字列から、数値に戻すには、
CLng( "&h" & Mid( "0x10", 3 ) )
32ビット整数 (C言語の int32_t) の値は、&h...& と記述します。
&h8000 = -32768 '// (Long型 = C言語の int16_t 型の値から型変換したもの)
&h8000& = 32768 '// (Long型 = C言語の int32_t 型の値)
&h10000 = 65536 '// (Long型 = C言語の int32_t 型の値)
←
▼
▲
Function InStr( [start_i,] s, key [,mode] ) as integer
(p211)
start_i
開始位置(1〜)、省略すると 1
s
検索対象の文字列
key
検索キーワード
返り値
見つかった位置(1〜)、 見つからなかった = 0
取得した返り値 i を Left( s, i ) とすると、見つかった文字まで含みます。
取得した返り値 i を Mid( s, i ) とすると、見つかった文字から含みます。
start_i を指定しても、返り値は s の先頭からの位置です。
【引数】
文字列 s の中を検索して、文字列 key のある位置を返します。
A B C D E F
1 2 3 4 5 6
InStr
Left(s,3) = "ABC"
Mid(s,3) = "CDEF"
参考
s が Null のときは、Null を返します。
大文字小文字を区別しないときは、s と key を LCase 関数で変換してください。
サンプル
Assert InStr( "abcde", "cd" ) = 3
Assert InStr( 1, "abcde", "CD", 1 ) = 3
補足
大文字小文字を区別しない
mode
大文字小文字を 0=区別する、1=区別しない
←
▼
▲
Function InStrRev( s, key [,start][,mode] ) as integer
start_i
開始位置、省略すると -1=末尾
s
検索対象の文字列
key
検索キーワード
返り値
見つかった位置(1〜)、 見つからなかった = 0
【引数】
文字列 s の中を末尾から検索して、文字列 key のある位置を返します。
mode
関連
大文字小文字を 0=区別する、1=区別しない
←
▼
▲
Function Replace( str as string, from as string, to as string
[, start[, count[, compare]]] )
str
置き換えられる文字列を含む文字列
from
置き換える前の文字列
to
置き換えた後の文字列
返り値
置き換えた後の文字列を含む文字列
【引数】
文字列の一部を置き換えます。
str に from と一致する文字列が複数あったら、すべて置き換えます。
例:
Replace( "abcd abcd", "abc", "x" ) = "xd xd"
関連
関連
Replace( "abcd ABCD", "abc", "x",1,-1,1 ) = "xd xD"
例:
←
▼
▲
Function Join( Array as array of variant [,Delimiter as string] ) as string
配列の要素を列挙した文字列を返します。
【引数】
Array
Delimiter
列挙する配列
要素の間に入れる文字列、省略時=" "(空白)
返り値
配列の要素を列挙した文字列
←
▼
▲
英文字を小文字にします。
Function LCase( s as string ) as string
←
▼
▲
Function Asc( Text as string ) as integer
文字列の先頭の文字を、Shift-JIS コードの数値で返します。
VBScript のテキストは Unicode ですが、Asc の返り値は Unicode ではありません。
サンプル
Assert Asc( "あ" ) = &h82A0
82 が Shift-JIS の第1バイト
&h82A0 はマイナスの値です。
←
▼
▲
Function Left( s as string, i as integer ) as string
文字列の左から数文字だけ返します。
s
i
返り値
"abc"
2
"ab"
"abc"
9
"abc"
0
""
"abc"
エラー 5
-1
"abc"
"あaいbうc"
4
"あaいb"
""
1
""
←
▼
▲
Function Mid( s as string, i as integer [, n as integer ] ) as string
文字列の i 文字目から n 文字分だけ返します。
s
i
返り値
"abc"
2
"b"
""
4
"abc"
0
"abc"
エラー 5
"あaいbうc"
3
"いb"
n
1
(省略)
"bc"
2
"abc"
2
"bc"
3
"abc"
1
2
1
0
""
1
"abc"
1
-1
"abc"
エラー 5
1
9
""
"abc"
←
▼
▲
Function StrComp( String1 as string, String2 as string [, Option as integer ] )
as integer
文字列を比較します。
Option に指定できる値
vbBinaryCompare (=0)
vbTextCompare (=1)
大文字小文字は区別しない
大文字小文字は区別する。(デフォルト)
サンプル: 下記の条件式は、すべて真です
If StrComp( "a", "b" ) < 0 Then ...
If StrComp( "a1", "a2" ) < 0 Then ...
If StrComp( "A", "a" ) < 0 Then ...
If StrComp( "A", "a", 1 ) = 0 Then ...
Select 文で、大文字小文字を区別しないときは、次のように UCase を使います。
Select Case UCase( str )
Case "ABC" : op = ABC
Case "DEF" : op = DEF
Case Else : op = Empty
End Select
返り値は、String1 = String2 なら、0 です。
←
▼
▲
Function UCase( s as string ) as string
英文字を大文字にします。
←
▼
▲
Class Book
Public m_Title '// メンバ変数
Private m__Price '// メンバ変数
Private Sub Class_Initialize() '// コンストラクタ
End Sub
Public Sub Echo() '// メンバ関数
WScript.Echo Title & " - " & Price
End Sub
End Class
main
Sub main()
Dim a_book
Set a_book = new Book : ErrCheck
a_book.Title = "VBS本"
a_book.Echo
a_book = Empty '// 明示的な廃棄
End Sub
Class
Public Sub
new
.
End Class
配列のメンバ変数もできます。
ただし、Redim は、メンバ関数の中でしかできません。
Public XArr()
Redim Preserve XArr(-1)
コンストラクタで初期化
構造体配列の初期化子に相当するものはありませんが、
Me
メンバ関数の中で使える、自分のオブジェクト(this ポインタ)
TypeName( obj )
クラス名を文字列で返す
クラスを関数の引数に渡すことはできません。 オブジェクトは渡せます。
If a Is b Then
同じオブジェクトかどうかを判定します。
Class Book
Private Sub Class_Initialize()
End Sub
Private Sub Class_Terminate()
End Sub
Public Property Get Title()
Title = ""
End Property
Public Property Let Title( value )
End Property
Public Property Set AnObj( obj )
End Property
Public Property Get Title( index )
Title = ""
End Property
End Class
Class_Terminate
Set ObjA.AnObj = ObjB ' Property Set AnObj が呼ばれる
プロパティにオブジェクトを設定するときは、次のように記述し、Property Set が呼ばれます。
Public Property Let XXXX( x ) : XXXX = x : End Property
Public Property Get XXXX() : XXXX = m_XXXX : End Property
Public Property Set XXXX( x ) : Set XXXX = x : End Property
Public Property Get XXXX() : If IsObject(m_XXXX) Then Set XXXX = m_XXXX : _
Else XXXX = m_XXXX : End If : End Property
' コンストラクタ
' プロパティ(Read)
' プロパティ(Write)
' プロパティ(Set)
' 添え字付き Read プロパティ
Property Get Title を Default Property Get Title に変えると、プロパティ名を省略
したときに Title プロパティが取得できます。
すれば使えます。
をサポート
関連
参考
委譲するとき
Public Property Let XXXX( x ) : m_Partner.XXXX = x : End Property
Public Property Get XXXX : XXXX = m_Partner.XXXX : End Property
Public Property Set XXXX( x ) : Set m_Partner.XXXX = x : End Property
Public Property Get XXXX : Set XXXX = m_Partner.XXXX : End Property
Public Sub XXXX( a ) : m_Partner.XXXX a : End Sub
Public Function XXXX( a ) : XXXX = m_Partner.XXXX( a ) : End Function