Results 1 to 5 of 5

Thread: about Replace$

  1. #1

    about Replace$

    suppose i have a text file in which its words should be separated by one space only, and i don't want some one to cheat by inserting other spaces, i know there is a regular expression solution for such cases, but i prefer a ready to use functions
    this is my approach using a string instead of a file, it seems to work, other solutions is appreciated.
    also why the text in the TextBox selected ?
    Uses "UI"
     
      Begin ControlID
        %ID_TextBox2
         
      End ControlID
         
      Function TBMain()  
        Dim hDlg, i As Long 
        Dim stringo As String
        stringo = "Goto          (goto , GOTO                    , GO TO or other case       combinations, depending on    the programming language) is a    statement found in many computer        programming languages. It performs a one-way transfer   of control to another line of code" 
        For i=1 To 100
        stringo = Replace$(stringo, "  ", " ")
        If InStr(1, Stringo, "  ") = 0 Then
        Exit For
        End If
        Next
        'msgbox 0, str$(i)
        Dialog New 0, "replace many spaces to one space", -1, -1, 500, 400, _
                                                      %WS_DLGFRAME Or %DS_CENTER Or %WS_CAPTION Or %WS_SYSMENU, _
                                                      0 To hDlg
     
        
        Control Add Textbox , hDlg, %ID_TextBox2, "test" , 5,  25, 450, 310,  %ES_WANTRETURN  Or _
                                                                              %ES_MULTILINE   Or _
                                                                              %ES_AUTOVSCROLL Or _ 
                                                                                            
         
    Control Append Text hDlg, %ID_TextBox2, $CRLF & stringo & $CRLF     
     
        '---Show dialog in MODAL
        Dialog Show Modal hDlg, Call dlgCallback
      End Function
       
      CallBack Function dlgCallback() As Long
       
        Select Case CBMSG
          Case %WM_CLOSE
        End Select 
       
      End Function
    
    EDIT:
    this is a better solution:
    replace:
    For i=1 To 100
        stringo = Replace$(stringo, "  ", " ")
        If InStr(1, Stringo, "  ") = 0 Then
        Exit For
        End If
        Next
    
    with:
    Repeat
        stringo = Replace$(stringo, "  ", " ")
        Until InStr(1, Stringo, "  ") = 0
    
    Last edited by primo; 23-12-2016 at 08:48.

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    Ciao,

    have a look at TrimFull$ too: http://www.thinbasic.com/public/products/thinBasic/help/html/index.html?trimfull$.htm

    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    Thank you Eros, this is unexpected that only stringo = TrimFull$(stringo) will remove All double spaces inside the stringo. very nice additions to thinbasic. and it makes the above code even shorter and speedier.
    Last edited by primo; 23-12-2016 at 14:10.

  4. #4
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,777
    Rep Power
    10
    There are a lot of built in string functions in thinBasic, developed over the years thanks to suggestions and contributions from many users. Built in functions are much more faster because they are compiled.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  5. #5
    This is the final version of the code above, added the Eros suggestion, also added :
    Control Set Focus hDlg, %ID_TextBox
    Control Send hDlg, %ID_TextBox, %EM_SETSEL, 0, 0

    to deselect the text in the TextBox. if we change the 0,0 to 1,0 then we select the first character, if we change it to 7,0 then what selected is test from the first line and G from the second line. since 4+1+$CRLF(2 chars) = 7
    Uses "UI"
    
    Begin ControlID
    %ID_TextBox
    
    End ControlID
    
    Function TBMain() 
    Dim hDlg, i As Long
    Dim MyStr As String
    MyStr = "Goto (goto , GOTO , GO TO or other case combinations, depending on the programming language) is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code"
    MyStr = TrimFull$(MyStr)
    
    Dialog New 0, "replace many spaces to one space", -1, -1, 500, 400, _
    %WS_DLGFRAME Or %DS_CENTER Or %WS_CAPTION Or %WS_SYSMENU, 0 To hDlg
    
    Control Add Textbox , hDlg, %ID_TextBox, "test" , 5, 25, 450, 310, %WS_TABSTOP Or %ES_WANTRETURN  Or %ES_MULTILINE 
    Control Send hDlg, %ID_TextBox, %EM_LIMITTEXT, 1000000, 0
    Control Append Text hDlg, %ID_TextBox, $CRLF & MyStr & $CRLF 
    Control Set Focus hDlg, %ID_TextBox
    Control Send hDlg, %ID_TextBox, %EM_SETSEL, 0, 0
    Static hFont2 As DWord Resource '---With Resource you will instruct thinBasic to 
    hFont2 = Font_Create("Courier New" , 20) '---Select a mono space font
    Control Send hDlg, %ID_TextBox, %WM_SETFONT, hFont2, 0
    Control Set Color hDlg, %ID_TextBox, -1, Rgb(100, 240, 140) 
    
    
    '---Show dialog in MODAL
    Dialog Show Modal hDlg, Call dlgCallback
    End Function
    
    CallBack Function dlgCallback() As Long
    
    Select Case CBMSG
    Case %WM_CLOSE
    End Select
    
    End Function
    
    remember if you want fonts with another sizes for the TextBox then look at my previous thread here http://www.thinbasic.com/community/s...teFont&p=93216
    Last edited by primo; 27-12-2016 at 20:28. Reason: added font type, and size

Similar Threads

  1. file and replace qt :)
    By lydia_sp in forum File
    Replies: 7
    Last Post: 15-09-2009, 18:28

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •