トッカンソフトウェア

WSH 文字列

文字列について書いていきます。


文字列を繋げる

				
dim str
str = "test1"
wscript.echo str

'文字列を繋げる
str = str + "add1"
str = str & "add2"
wscript.echo str


			

文字列を切り出す(部分指定)

				
dim str
str = "1234567890"

'文字の抜き出しはMid関数(文字列,開始位置,長さ)
'↓は"34"が出力される
wscript.echo Mid(str,3,2)

'Left関数、Right関数もある。両方とも文字列、長さを指定する
'↓は"12"が出力される
wscript.echo Left(str,2)

'↓は"890"が出力される
wscript.echo Right(str,3)


			

文字列の長さ

				
dim str
str = "1234567890"

'長さはLen関数。全角、半角ともに文字数を返す
'↓は10が出力される
wscript.echo Len(str)


			

文字列の比較

				
dim str
str = "1234567890"

'比較はそのまま = でいけます
if str = "1234567890" then
	wscript.echo "true"
end if

'含まれるかは InStr関数でいけます
if InStr("1234567890","1") > 0  then
	wscript.echo "true"
end if


			

指定文字の位置

				
dim str
str = "12345678901234567890"

'文字の位置は InStr 関数で調べます
wscript.echo InStr(str,"234")

'後ろから調べる場合は InStrRev 関数で調べます
wscript.echo InStrRev(str,"234")


			

文字列の置換

				
dim str
str = "12345678901234567890"

'文字の置換は Replace 関数で行います
wscript.echo Replace(str, "234", "abc")



			

大文字小文字変換

				
dim str
str = "Abc"

'大文字に変換するには UCase 関数を使います
wscript.echo UCase(str)

'小文字に変換するには LCase 関数を使います
wscript.echo LCase(str)


			

スペース削除

				
dim str
str = "   Abc   "

'スペース削除はTrim関数を使います
wscript.echo "-" + Trim(str) + "-"

'スペース削除はRTrim関数を使います
wscript.echo "-" + RTrim(str) + "-"

'スペース削除はLTrim関数を使います
wscript.echo "-" + LTrim(str) + "-"


			

文字列→数値

				
dim str
str = "123"

'文字列を数値に変換するにはCInt関数(Integer)かCLng関数(Long)かCDbl関数(Double)を使います
wscript.echo CInt(str) + CInt(str)
wscript.echo CLng(str) + CLng(str)
wscript.echo CDbl(str) + CDbl(str)



			

指定文字の分割

				
dim strs
dim str
dim i

'指定文字の分割にはSplit関数を使います
strs =  Split("abc def ghi", " ")

For Each str In strs
	wscript.echo str
Next

for i = 0 to UBound(strs)
	wscript.echo strs(i)
next


			


ページのトップへ戻る