忍者ブログ

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

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

BHT-BASIC4.0:文字列を扱うユーザ定義関数についてその3
さらに、文字列を扱うユーザ定義関数として使えそうなものを作成してみました。
関数定義引数戻り値
指定バイト数内に収まる文字列を戻す
Function GfCut$(pstrValue$, pintMax%)[255]
pstrValue$:対象文字列
pintMax%:指定バイト
GfCut$:結果文字列
横幅一杯でのテキスト表示
Sub GsPrintFull(Byval pstrSrc$)
pstrSrc$:対象文字列

'---------------------------------------
'指定バイト数内に収まる文字列を戻す関数
'---------------------------------------
'Function GfCut$(pstrValue$, pintMax%)[255]
'引 数:
'	pstrValue$:対象文字列
'	pintMax%:指定バイト数
'戻り値:
'	GfCut$	:結果文字列
'---------------------------------------
	Function GfCut$(pstrValue$, pintMax%)[255]
		'エラー処理宣言
		On Error Goto GfCut.ErrProc

		Private intPos%, intErr%, intLoop%, intChr%, strRet$[255]
		intPos% = 0
		intErr% = GcFalse%
		intLoop% = GcTrue%
		While intLoop% = GcTrue%
			'ASCIIコード取得
			intChr% = Asc(Mid$(pstrValue$, intPos% + 1, 1))
			'2バイト文字(先頭バイトがSJISコード??)
			If &H80 <= intChr% And intChr% <= &H9F Then
				intPos% = intPos% + 2
				If intPos% > pintMax% Then
					'最大バイト長を超える場合、前の状態に戻すフラグON
					intErr% = GcTrue%
				End If
				If intPos% >= pintMax% Then
					'最大バイト長に達する場合は、その場で終了
					If intErr% = GcTrue% Then
						strRet$ = Mid$(pstrValue$, 1, intPos% - 2)
					Else
						strRet$ = Mid$(pstrValue$, 1, intPos%)
					End If
					intLoop% = GcFalse%
				End If
			'1バイト文字
			Else
				intPos% = intPos% + 1
				If intPos% >= pintMax% Then 
					'最大バイト長に達する場合は、その場で終了
					strRet$ = Mid$(pstrValue$, 1, intPos%)
					intLoop% = GcFalse%
				End If
			End If
		WEnd
		GfCut$ = strRet$
GfCut.Return	'関数戻り
		On Error Goto 0
		Exit Function
'-----
'エラー処理
'-----
GfCut.ErrProc
		GfCut$ = ""
		Resume GfCut.Return
	End Function

	const	McMaxColumns% = 20		'画面の半角桁数MAX
'---------------------------------------
'横幅一杯でのテキスト表示
'---------------------------------------
'Sub GsPrintFull(Byval pstrSrc$)
'引 数:
'	pstrSrc$:対象文字列
'---------------------------------------
    Sub GsPrintFull(Byval pstrSrc$)
		'エラー処理宣言
		On Error Goto GsPrintFull.ErrProc

		Private strWK$[255]
		strWK$ = GfCut$(pstrSrc$ + "                      ", McMaxColumns%)
    	If Len(strWK$) < McMaxColumns% Then
			strWK$ = strWK$ + LEFT$("          ", McMaxColumns% - Len(strWK$))
		Endif
		Print strWK$;
GsPrintFull.Return	'関数戻り
		On Error Goto 0
		Exit Sub
'-----
'エラー処理
'-----
GsPrintFull.ErrProc
		Resume GsPrintFull.Return
	End Sub

尚、GfCut$関数は、以前存在した「ハンディターミナル(BHT)の部屋」というサイトからの引用です。
また、GsPrintFull関数は画面の横幅を20文字と限定しています。(フォントの設定が24ドット)



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











PR

コメント

コメントを書く