忍者ブログ

VB.NET-TIPS などプログラミングについて

VB.NETのTIPS(小技集)を中心に、Javascript、PHP その他のプログラミングについて少し役に立つ情報を発信します。いわゆる個人的な忘備録ですが、みなさんのお役に立てれば幸いです。

BHT-BASIC4.0:文字列を扱うユーザ定義関数についてその2
文字列を扱うユーザ定義関数として使えそうなものを作成してみました。
関数定義引数戻り値
指定された文字列を分離文字列で分解
Sub GsSplit$(byval pstrSrc$, Byval pstrSplit$, Byref parr$())
pstrSrc$:分離元文字列
pstrSplit$:分離文字列
parr$:分解先配列
 
指定文字で埋めて、右詰又は左詰で返す
'Function GfPadd$(pintMode%, pstrBuf$, pintLen%, pstrPadChar$)[255]
pintMode%:左詰(GcTrue%),右詰(GcFalse%)
pstrBuf$:文字列
pintLen%:最終文字列長(最大255)
pstrPadChar$:埋める文字
GfPadd$:結果文字列

'---------------------------------------
'指定された文字列を分離文字列で分解する
'---------------------------------------
'Sub GsSplit$(byval pstrSrc$, Byval pstrSplit$, Byref parr$())
'引 数:
'	pstrSrc$	:分離元文字列
'	pstrSplit$	:分離文字列
'	parr$		:分解先配列
'---------------------------------------
	Sub GsSplit$(byval pstrSrc$, Byval pstrSplit$, Byref parr$())
		'エラー処理宣言
		On Error Goto GsSplit.ErrProc

		Private i%: i% = 1
		Private pos%: pos% = 1
		Private posLast%: posLast% = 1
		
		'区切り文字列を探す
		pos% = INSTR(posLast%, pstrSrc$, pstrSplit$)
		While pos% > 0
			'前回位置から、区切り文字列の前までを退避
			parr$(i%) = MID$(pstrSrc$, posLast%, pos% - posLast%)
			'指標++
			i% = i% + 1
			'前回位置を区切り文字列の次にする
			posLast% = pos% + LEN(pstrSplit$)
			'区切り文字列を探す
			pos% = INSTR(posLast%, pstrSrc$, pstrSplit$)
		Wend
		'最後の文字列
		parr$(i%) = MID$(pstrSrc$, posLast%)
GsSplit.Return	'関数戻り
		On Error Goto 0
		Exit Sub
'-----
'エラー処理
'-----
GsSplit.ErrProc
		Resume GsSplit.Return
	End Sub

'---------------------------------------
'指定文字で埋めて、右詰又は左詰で返す
'---------------------------------------
'Function GfPadd$(pintMode%, pstrBuf$, pintLen%, pstrPadChar$)[255]
'引 数:
'	pintMode%	:左詰(GcTrue%)か右詰(GcFalse%)か
'	pstrBuf$	:文字列
'	pintLen%	:最終文字列長(最大255)
'	pstrPadChar$:埋める文字
'戻り値:
'	GfPadd$		:結果文字列
'---------------------------------------
	Function GfPadd$(pintMode%, pstrBuf$, pintLen%, pstrPadChar$)[255]
		'エラー処理宣言
		On Error Goto GfPadd.ErrProc

		Private intPad%, strTmp$[255]
		intPad% = pintLen% - Len(pstrBuf$)
		If intPad% > 0 Then
			strTmp$ = GfString$(pstrPadChar$, intPad%)
			If pintMode% = GcTrue% Then
				'左詰
				GfPadd$ = pstrBuf$ + strTmp$
			Else
				'右詰
				GfPadd$ = strTmp$ + pstrBuf$
			End If
		Else
			'そのまま返す
			GfPadd$ = pstrBuf$
		End If
GfPadd.Return	'関数戻り
		On Error Goto 0
		Exit Function
'-----
'エラー処理
'-----
GfPadd.ErrProc
		GfPadd$ = ""
		Resume GfPadd.Return
	End Function

これらの関数の実行ソースは以下の様になります。
'-----
'実行はここから処理
'-----
Main
	SCREEN 1				'漢字モード
	LOCATE , , 2			'カーソルをブロック表示

	PRINT "***GfPadd$"
	PRIVATE W$, W2$
	W$ = GfPadd$(GcTrue%, "123", 5, " ")
	PRINT "[" + W$ + "]"
	W$ = GfPadd$(GcFalse%, "123", 5, "0")
	PRINT "[" + W$ + "]"

	PRINT "***GsSplit$"
	PRIVATE ARR$(10)
	CALL GsSplit$("123,AAA,!!!!!!!", ",", ARR$)
	FOR I% = 1 TO 3
		PRINT "ARR$("; I%;
		PRINT ")="; ARR$(I%)
	NEXT
	WAIT 0, &h01 'キー入力待ち
	END
これを実行すると以下の様な表示になります。



=====
2016/04/02:の時の情報











PR

コメント

コメントを書く