Escaping single or double quotes

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Nathan.P
Posts: 4
Joined: Sat Mar 11, 2017 7:12 pm

Escaping single or double quotes

Post by Nathan.P »

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
OpenOffice 4.1.3 - Win7
User avatar
Zizi64
Volunteer
Posts: 11359
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Escaping single or double quotes

Post by Zizi64 »

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.
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.
JeJe
Volunteer
Posts: 2779
Joined: Wed Mar 09, 2016 2:40 pm

Re: Escaping single or double quotes

Post by JeJe »

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


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
From:
http://www.xbeat.net/vbspeed/c_Replace.htm
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply