Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strtran() to search & replace quotation marks

Status
Not open for further replies.

MForrest

Programmer
Jun 9, 2003
85
GB
Afternoon all, I am trying to search for all occurrences of double quotation marks that may be entered by a user and replace them with single quotations. The point of this is when I export and import my data as a csv file the data is corrupt on import as the use of double quotes are mistaken for data separators. To do this I naively tried to use strtran() which I believe is not possible when trying to find and replace double or single quotes. Heres the code I tried to use, any ideas??

for a = 1 to array_size

if vartype( test_array( a)) == "C"
private find_replace
find_replace = test_array( a)
strtran(find_replace, '"', ''')
test_array( a) = find_replace
endif
next a
 
What about using memvars to hold the single and double quotes in the STRTRAN() instead of literals:

SQuote = "'"
DQuote = '"'

replace all find_replace with STRTRAN(find_replace,DQuote,SQuote)

This worked in brief test, need to adapt to your array-based code - give it a try.......

Dennis
 
OOps, something went wrong with the code I just posted. I meant put square brackets round both the quote marks, but I think the Tek-Tips TGML processor took them out.

I'll try again:

[ignore]
x = strtran(x,["],['])
[/ignore]

Mike

Mike Lewis
Edinburgh, Scotland
 
Cheers guys, I have the strtran function working, using the square brackets!!

Another day another dollar
[thumbsup2]
 
I use CHR(39) for single quotes and CHR(34) for double quotes. Using the ASCII map often gets around illegal characters in code lines...

x = strtran(x,CHR(34),CHR(39))

Brian
 
Not to belabor a point, but the real key is to just use a different character for the embedded quotes so that the parser can figure out where a string starts or ends...all the possible options have pretty much been gone over in this thread but I thought I would recap the possible delimiters that signify a string to VFP...

'String' &&Single-quote
"String" &&Double-quote
[String] &&Bracket

...and baltman brings up another good point, when all else fails maybe incorporate some ascii in order to solve a problem.

cString = " 'Hello World!' |" + ' "Goodbye World!" |' + [ ' "This is Crazy!" ']
?cString
?STRTRAN(x,["],[']) && One way of doing it
?STRTRAN(x,CHR(34),CHR(39)) && Another way of doing it
?STRTRAN(cString,'"',"'") && Yet another way of doing it

...one of the reasons knowing this is important is because you will most likely find times with SQL strings where you need to embed quotes and have them parsed properly. The solution that Mike laid out is pretty good using the brackets since "'" can sometimes blend together and be hard to read or visually parse, and ascii codes are not always easy to remember off the top of your head. Those are my thoughts for what they're worth.

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Arggggggggh!!! You'd think I would have learned from Mike's mistake:

Code:
cString = " 'Hello World!' |" + ' "Goodbye World!" |' + [ ' "This is Crazy!" ']
?cString
?STRTRAN(x,["],[']) && One way of doing it
?STRTRAN(x,CHR(34),CHR(39)) && Another way of doing it
?STRTRAN(cString,'"',"'") && Yet another way of doing it

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Slighthaze

Not to belabor a point, ;-) but should this:
?STRTRAN(x,CHR(34),CHR(39)) && Another way of doing it

read
?STRTRAN(cString,CHR(34),CHR(39)) && Another way of doing it



Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
 
Slighthaze,

Arggggggggh!!! You'd think I would have learned from Mike's mistake:

The solution to that particular problem was to put IGNORE in square brackets just begore the code, and the same, but with a forward slash in front of IGNORE just after it. But I assume you discovered that for yourself.

Nothing like finding out the hard way.

Mike


Mike Lewis
Edinburgh, Scotland
 
mgagnon,

Yes it should...

cString = " 'Hello World!' |" + ' "Goodbye World!" |' + [ ' "This is Crazy!" ']
?cString
?STRTRAN(cString,["],[']) && One way of doing it
?STRTRAN(cString,CHR(34),CHR(39)) && Another way of doing it
?STRTRAN(cString,'"',"'") && Yet another way of doing it

MikeLewis,

I used the
Code:
tags but the result is the same as [ignore][/ignore] I think.

Slighthaze = NULL

[ul][li]FAQ184-2483
An excellent guide to getting a fast and accurate response to your questions in this forum.[/li][/ul]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top