PDA

View Full Version : about Replace$



primo
22-12-2016, 15:29
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

ErosOlmi
23-12-2016, 11:51
Ciao,

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

Eros

primo
23-12-2016, 14:08
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.

ErosOlmi
23-12-2016, 15:56
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

primo
24-12-2016, 16:49
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/showthread.php?12720-about-CreateFont&p=93216