文字列を扱うユーザ定義関数としてよくある感じのものを作成してみました。
以下のソースが5個の関数を宣言しています。
エラー処理を各関数に入れましたが、必要ないかもしれません。 また、引数の文字列の長さチェックは行っていませんので、必要ならば追加して下さい。
これらの関数の実行ソースは以下の様になります。
=====
2016/04/02:の時の情報
関数定義 | 引数 | 戻り値 |
---|---|---|
半角空白文字列生成Function GfSpc$(byval pintCnt%)[128] |
pintCnt%:文字数を指定 | GfSpc$:空白文字列 |
右側半角空白削除Function GfRTrim$(pstrValue$)[255] |
pstrValue$:対象文字列 | GfRTrim$:結果文字列 |
左側半角空白削除Function GfLTrim$(pstrValue$)[255] |
pstrValue$:対象文字列 | GfRTrim$:結果文字列 |
前後の半角空白削除Function GfTrim$(pstrValue$)[255] |
pstrValue$:対象文字列 | GfTrim$:結果文字列 |
文字列繰り返しFunction GfString$(pintSize%, pstrChr$)[255] |
pstrChr$:対象文字 pintSize%:文字数(最大1~255) |
GfString$:結果文字列 |
エラー処理を各関数に入れましたが、必要ないかもしれません。 また、引数の文字列の長さチェックは行っていませんので、必要ならば追加して下さい。
'--------------------------------------- '定数宣言:共通⇒(これは別ファイルにすべき) '--------------------------------------- Global GcTrue% : GcTrue% = -1 Global GcFalse% : GcFalse% = 0 '--------------------------------------- '半角空白文字列生成 '--------------------------------------- 'Function GfSpc$(byval pintCnt%)[128] '引 数: ' pintCnt% :文字数を指定 '戻り値: ' GfSpc$ :空白文字列 '--------------------------------------- Function GfSpc$(byval pintCnt%)[128] Private strSpc$[128]: strSpc$ = "" Private i% If pintCnt% > 128 Then pintCnt% = 128 Endif For i%=1 To pintCnt% strSpc$ = strSpc$ + " " Next GfSpc$ = strSpc$ End Function '--------------------------------------- '右側半角空白削除(Rtrim) '--------------------------------------- 'Function GfRTrim$(pstrValue$)[255] '引 数: ' pstrValue$ :対象文字列 '戻り値: ' GfRTrim$ :結果文字列 '--------------------------------------- Function GfRTrim$(pstrValue$)[255] 'エラー処理宣言 On Error Goto GfRTrim.ErrProc Private intCnt%, intIdx% '最後尾から空白以外の文字を探す intCnt% = 0 For intIdx% = Len(pstrValue$) To 1 Step -1 If Mid$(pstrValue$, intIdx%, 1) <> " " Then intCnt% = intIdx% intIdx% = 1 'ループを止める Endif Next '右側全ての空白を排除 GfRTrim$ = Left$(pstrValue$, intCnt%) GfRTrim.Return '関数戻り On Error Goto 0 Exit Function '----- 'エラー処理 '----- GfRTrim.ErrProc GfRTrim$ = "" Resume GfRTrim.Return End Function '--------------------------------------- '左側半角空白削除(Rtrim) '--------------------------------------- 'Function GfLTrim$(pstrValue$)[255] '引 数: ' pstrValue$ :対象文字列 '戻り値: ' GfLTrim$ :結果文字列 '--------------------------------------- Function GfLTrim$(pstrValue$)[255] 'エラー処理宣言 On Error Goto GfLTrim.ErrProc Private intCnt% strTemp$ = "" '--- 左端からスペースではない文字を探していく For intCnt% = 1 To Len(pstrValue$) If Mid$(pstrValue$, intCnt%, 1) <> " " Then '空白では無い文字以降の文字列を戻す GfLTrim$ = Mid$(pstrValue$, intCnt%) intCnt% = Len(pstrValue$) End If Next intCnt% GfLTrim.Return '関数戻り On Error Goto 0 Exit Function '----- 'エラー処理 '----- GfLTrim.ErrProc GfLTrim$ = "" Resume GfLTrim.Return End Function '--------------------------------------- '前後の半角空白削除(Trim) '--------------------------------------- 'Function GfTrim$(pstrValue$)[255] '引 数: ' pstrValue$ :対象文字列 '戻り値: ' GfTrim$ :結果文字列 '--------------------------------------- Function GfTrim$(pstrValue$)[255] Private strWK$[255] strWK$ = GfLTrim$(pstrValue$) '左側空白削除 GfTrim$ = GfRTrim$(strWK$) '右側空白削除 End Function '--------------------------------------- '文字列繰り返し '--------------------------------------- 'Function GfString$(pintSize%, pstrChr$)[255] '引 数: ' pstrChr$ :対象文字 ' pintSize% :文字数(最大1~255) '戻り値: ' GfString$ :結果文字列 '--------------------------------------- Function GfString$(pstrChr$, pintSize%)[255] 'エラー処理宣言 On Error Goto GfString.ErrProc Private intCnt%, strTemp$[255] strTemp$ = "" '文字数分の文字列連結 For intCnt% = 1 To pintSize% strTemp$ = strTemp$ + pstrChr$ Next intCnt% GfString$ = strTemp$ GfString.Return '関数戻り On Error Goto 0 Exit Function '----- 'エラー処理 '----- GfString.ErrProc GfString$ = "" Resume GfString.Return End Function
これらの関数の実行ソースは以下の様になります。
Sub DispData(Byval strP1$, Byval strP2$) PRINT "[" + strP1$ + "]=>[" + strP2$ + "]" End Sub '----- '実行はここから処理 '----- Main ' PRIVATE Num$, nRet%, intLoop% SCREEN 1 '漢字モード LOCATE , , 2 'カーソルをブロック表示 PRINT "***GfRTrim$" PRIVATE W$, W2$ W$ = "1234 " W2$ = GfRTrim$(W$) CALL DispData(W$, W2$) W$ = "12 34 " W2$ = GfRTrim$(W$) CALL DispData(W$, W2$) W$ = "12 34 A" W2$ = GfRTrim$(W$) CALL DispData(W$, W2$) PRINT "***GfLTrim$" W$ = " 1234" W2$ = GfLTrim$(W$) CALL DispData(W$, W2$) W$ = " 12 34 " W2$ = GfLTrim$(W$) CALL DispData(W$, W2$) W$ = " 1234 A" W2$ = GfLTrim$(W$) CALL DispData(W$, W2$) PRINT "***GfTrim$" W$ = " 1234 " W2$ = GfTrim$(W$) CALL DispData(W$, W2$) W$ = " 12 34 " W2$ = GfTrim$(W$) CALL DispData(W$, W2$) PRINT "***GfString$" W$ = GfString$("@", 10) PRINT W$; WAIT 0, &h01 'キー入力待ち ENDこれを実行すると以下の様な表示になります。
=====
2016/04/02:の時の情報
PR
コメント