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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for string containing - [ ] '' or " "

Status
Not open for further replies.

KALP1

Programmer
Aug 26, 2016
102
IN
I need to filter records on a character expression. I have written
a1 = "KUM'AR"
lcLikeExpr = [LIKE('*] + A1 + [*',Particular)]
SET FILTER TO &lcLikeExpr
** This is giving missing operand. As soon as variable a1 includes single apostrophy, I get error missing operand.

If I change this to
a1 = "KUM'AR"
lcLikeExpr = [LIKE("*] + A1 + [*",Particular)]
SET FILTER TO &lcLikeExpr
Then problem is solved for a1 = "KUM'AR", but error occurs for a1 = 'KUM"AR'.



 
Code:
lcLikeExpr = [LIKE('*] + STRTRAN(A1,"'",['+"'"+']) + [*',Particular)]
 
Yes, and there is no escaping in VFP, you're actually only busted, when ",' and [ or ] occur in the input.
But even if we only had ' as string delimiter and no escaping, we can do
Code:
a1 = "KUM'AR" && just as example
A1 = STRTRAN(A1,['],[[highlight #FCE94F]'[/highlight]+CHR(39)+[highlight #FCE94F]'[/highlight]]) && important: Only works with following expression using ' as delimiter:
lcLikeExpr = [LIKE([highlight #FCE94F]'[/highlight]*] + A1 + [*[highlight #FCE94F]'[/highlight],Particular)]
SET FILTER TO &lcLikeExpr

The highlighted quotation marks will begin/end a string expression in the final lcLikeExpr.

To make it clear, the final lcLikeExpr would be [tt]LIKE('*KUM'AR*',Particular)[/tt] (not working) vs [tt]LIKE('*KUM'+CHR(39)+'AR*',Particular)[/tt] (working)

You could also go about this with a case statement testing whether the A1 input has no ', no " or no [] in it and branch into code setting lcLikeExpression with the unused delimiter, but finally that breaks with input having all of them, the escape is using CHR().

Edit: Yes, or instead of +CHR(39)+ simply use +"'"+ - might be simpler and faster. Still the idea is the same: To puzzle together a string with parts not having a delimiter conflict.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top