I'm looking for two functions to escaped special characters like single or double quotes.
Example where string contains: My name is O'Reilly
Outputs: My name is O\'Reilly
Example where string contains: My name is "Joe"
Outputs: My name is \"Joe\"
I don't find in Basic instructions for that
Thanks
Escaping single or double quotes
Escaping single or double quotes
OpenOffice 4.1.3 - Win7
Re: Escaping single or double quotes
InStr() to determine the position of the quote character inside of a string
Mid() procedure to replace the quote character to an another character combination.
Mid() procedure to replace the quote character to an another character combination.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Re: Escaping single or double quotes
Instead of reinventing the wheel, search for an existing algorithm for these things. You'll probably find one written in VB6 which is almost identical to OOBasic. The below function is VB6 code from VBSpeed. OOBasic doesn't allow optional variables to be declared with a value so I've had to make slight changes to accommodate that - but very little adaptation is usually needed.
If you want to do something with regular expressions use Service XTextSearch
From:
http://www.xbeat.net/vbspeed/c_Replace.htm
If you want to do something with regular expressions use Service XTextSearch
Code: Select all
Sub Main
DIM ST
ST =" My name is O'Reilly"
msgbox replace01(ST,"'","\'",1,-1)
ST =" My name is " & CHR(34) & "Joe" & CHR(34)
MSGBOX replace01(ST,CHR(34),"\" & CHR(34),1,-1)
End Sub
Public Function Replace01(Expression As String, _
sOld As String, _
sNew As String, _
Optional ByVal Start As Long , _
Optional Count As Long, _
Optional CompareO As long ) As String
' by Donald, donald@xbeat.net, 20000918
Dim cntReplace As Long
Dim lenOld As Long
Dim lenNew As Long
if ismissing(start) =true then start =1
if ismissing(count) =true then count = -1
if ismissing(compareO) =true then compareO =vbBinaryCompare
Replace01 = Expression
lenOld = Len(sOld)
If lenOld Then
lenNew = Len(sNew)
' silently correct illegal Start value
If Start < 1 Then
Start = 1
End If
Do Until cntReplace = Count
Start = InStr(Start, Replace01, sOld, CompareO)
If Start Then
' replace
Replace01 = Left$(Replace01, Start - 1) & sNew & Mid$(Replace01, Start + lenOld)
cntReplace = cntReplace + 1
' next pos
Start = Start + lenNew
Else
Exit Do
End If
Loop
End If
End Function
http://www.xbeat.net/vbspeed/c_Replace.htm
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)