文字列の置換(U)          <TOP>


文字列置換関数

Replace(元の文字列, 修正する文字列, 置換文字列)

「declare function」を「Declare Function」に置換する例を示しています。

 
'================================================================
'= 文字列の置換(U)(置換関数)
'=    (Replace.bas)
'================================================================
#include "Windows.bi"

Var Shared Text(4) As Object
Var Shared Edit(2) As Object

For i = 0 To 4
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1)))
    If i < 2 Then
        Text(i).SetFontSize 14
    Else
        Text(i).SetFontSize 12
    End If
Next
For i = 0 To 2
    Edit(i).Attach GetDlgItem("Edit" & Trim$(Str$(i + 1)))
    Edit(i).SetFontSize 14
Next

Var Shared FL As String
Var Shared Buffer As String

'================================================================
'= 置換関数
'= sOriginal:原本 sCorrection:修正文字 sReplace:置換文字 
'================================================================
Declare Function Replace(strOrgriginal As String, strCororrection As String, strRepeplace As String) As String
Function Replace(strOrgriginal As String, strCororrection As String, strRepeplace As String) As String
    Var txt As String
    Var iPos As Integer

    txt = ""
    Do
        iPos = InStr(strOrgriginal, strCororrection)
        If iPos = 0 Then Exit do

        txt = txt & Left$(strOrgriginal, iPos - 1) & strRepeplace
        strOrgriginal = Mid$(strOrgriginal, iPos + Len(strCororrection))
    Loop

    txt = txt & strOrgriginal
    Replace = txt
End Function

'================================================================
'= 置換実行
'================================================================
Declare Sub BmpButton2_on edecl ()
Sub BmpButton2_on()
    If Edit(0).GetWindowText = "" Then
        A% = MessageBox("Attention!", "検索文字列を指定してください!", 0, 2)
        Exit Sub
    End If
 
    Var strOrg As String
    Var strCor As String
    Var strRep As String

    strOrg = Edit(2).GetWindowtext                                'バッファ内の文字列
    strCor = Edit(0).GetWindowtext                                '検索文字列
    strRep = Edit(1).GetWindowtext                                '置換後文字列

    '----------------------------------------
    Buffer = Replace(strOrg, strCor, strRep)
    '----------------------------------------

    EDit(2).SetWindowText Buffer
    SetFocus
End Sub

'================================================================
'= ファイルを開く
'================================================================
Declare Sub BmpButton1_on edecl ()
Sub BmpButton1_on()
    Var hFile As Integer

    For i = 0 To 2
        Edit(i).SetWindowText ""
    Next

    FL = WinOpenDlg("ファイルのオープン", "*.txt;*.bas;*.htm", "テキスト(*.txt);basファイル(*.bas);htmファイル(*.htm)", 0)

    If FL <> Chr$(&H1B) Then
        hFile = FreeFile
        Open FL For BinInp As hFile
        Buffer = Space$(Lof(hFile))
        FRead hFile, Buffer
        Buffer = JConv$(Buffer, 0, 2)

        Edit(2).SetWindowText Buffer
        Close hFile
    Else
        FL = ""
    End If
End Sub

'================================================================
'= 名前を付けて保存
'================================================================
Declare Sub BmpButton3_on edecl ()
Sub BmpButton3_on()
    Var hFile As Integer

    FL = WinSaveDlg("名前を付けて保存", FL, "テキスト(*.txt);basファイル(*.bas);htmファイル(*.htm)", 0)

    If FL <> Chr$(&H1B) Then
        hFile = FreeFile
        Open FL For BinIO As hFile

        If Lof(hFile) <> 0 Then
            Close hFile
            Kill FL
            Open FL For BinIO As hFile
        End If

        fWrite hFile, Buffer
        Close hFile
    End If
End Sub

'================================================================
'= リサイズ
'================================================================
Declare Sub MainForm_Resize edecl ()
Sub MainForm_Resize()
    Line(   0,   1) - (GetWidth,   1), , 1 : Line(   0,   2) - (GetWidth,   2), , 15
    Line(   0,  33) - (GetWidth,  33), , 1 : Line(   0,  34) - (GetWidth,  34), , 15

    If GetWidth < 435 Or GetHeight < 280 Then
        SetWindowSize 435, 280
    End If

    Edit(0).SetWindowSize GetWidth - 134, 24
    Edit(1).SetWindowSize GetWidth - 134, 24
    Edit(2).SetWindowSize GetWidth - 32, GetHeight - 148
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End