Lazarus

Programming => General => Topic started by: maurobio on December 09, 2019, 12:55:25 pm

Title: [SOLVED] Handling strings with single quotes
Post by: maurobio on December 09, 2019, 12:55:25 pm
Dear ALL,

I have strings of words delimited by single quotes and separated spaces. like that:

'7 setae' '9 setae' '10 setae'

How can I parse such strings into words, given that single quotes are used by the Pascal compiler itself as a string delimiter?

In the above case, I would like to parse the string into three words: '7 setae', '9 setae', '10 setae'.

Thanks in advance for any assistance you can provide.

Best regards,
Title: Re: Handling strings with single quotes
Post by: marcov on December 09, 2019, 01:25:30 pm
double the single quotes (one->two single quotes) inside the string
Title: Re: Handling strings with single quotes
Post by: lucamar on December 09, 2019, 01:45:03 pm
I haven't tested it but this should work:

Code: Pascal  [Select][+][-]
  1. functionParseQuoted(const AnString: String): String;
  2. var
  3.   AnArray: array of String;
  4. begin
  5.   AnArray := AnString.Split([''''], ExcludeEmpty);
  6.   Result := Result.Join(',', AnArray);
  7. end;
Title: Re: Handling strings with single quotes
Post by: maurobio on December 09, 2019, 03:04:35 pm
@lucamar,

Thank you very much. I always forget that FPC/Lazarus has these handy "Split" and "Join" Python-like functions!

However, I could not test your function because the compiler returned an "Identifier not found" for "ExcludeEmpty".

Best regards,
Title: Re: Handling strings with single quotes
Post by: MarkMLl on December 09, 2019, 03:16:36 pm
Forum post https://forum.lazarus.freepascal.org/index.php?topic=42356.0 (https://forum.lazarus.freepascal.org/index.php?topic=42356.0) might be relevant.

MarkMLl
Title: Re: Handling strings with single quotes
Post by: maurobio on December 09, 2019, 03:33:25 pm
@MarkML,

Might be, but it isn't. I tried this:

Code: Pascal  [Select][+][-]
  1. {$SCOPEDENUMS ON}
  2.   TStringSplitOptions = (None, ExcludeEmpty);
  3. {$SCOPEDENUMS OFF}
  4.  
And now the compiler complains of a syntax error: "BEGIN" expected but "identifier TSTRINGSPLITOPTIONS" found.

Regards,
Title: Re: Handling strings with single quotes
Post by: lucamar on December 09, 2019, 04:56:00 pm
Forgot about the scoped enum; try like this:

Code: Pascal  [Select][+][-]
  1. functionParseQuoted(const AnString: String): String;
  2. var
  3.   AnArray: array of String;
  4. begin
  5.   AnArray := AnString.Split([''''], TStringSplitOptions.ExcludeEmpty);
  6.   Result := Result.Join(',', AnArray);
  7. end;
Title: Re: Handling strings with single quotes
Post by: maurobio on December 09, 2019, 07:47:15 pm
@lucamar,

Unfortunately, still no luck with TStringSplitOptions.ExcludeEmpty!

Best regards,
Title: Re: Handling strings with single quotes
Post by: lucamar on December 09, 2019, 08:36:49 pm
Unfortunately, still no luck with TStringSplitOptions.ExcludeEmpty!

Hmm ... I'll have to make a real test, then; I don't remember having that problem but I only ever used it once some (longish) time ago.

In the meantime, if there are not many origin strings, they are relatively short and you can be fairly sure all are well-formed, you could try using StringReplace (or any of its cousins) to replace all ocurrences of '' by ',':

Code: Pascal  [Select][+][-]
  1. functionParseQuoted(const AnString: String): String;
  2. const
  3.   Quote = '''';
  4.   DQuote = Quote + Quote;
  5.   PlusComma = ''',''';
  6. begin
  7.   Result := StringReplace(AnString, DQuote, PlusComma, [rfReplaceAll]);
  8. end;
Title: Re: Handling strings with single quotes
Post by: winni on December 09, 2019, 08:56:53 pm
@lucamar

There is a blank between the items!

Code: Pascal  [Select][+][-]
  1. DQuote :=  Quote + #32 + Quote;

But after detecting the space you can just replace the space with a   ,

Winni
Title: Re: Handling strings with single quotes
Post by: maurobio on December 09, 2019, 10:06:29 pm
@lucamar,

This time your function worked nicely (with the modification proposed by @winni)!

Here is my test code:

Code: Pascal  [Select][+][-]
  1. program Test;
  2.  
  3. {$MODE OBJFPC}
  4. {$APPTYPE CONSOLE}
  5.  
  6. uses Classes, SysUtils, StrUtils;
  7.  
  8. var
  9.    S: string;
  10.  
  11. function ParseQuoted(const AnString: String): String;
  12. const
  13.   Quote = '''';
  14.   DQuote = Quote + #32 + Quote;
  15.   PlusComma = ''',''';
  16. begin
  17.   Result := StringReplace(AnString, DQuote, PlusComma, [rfReplaceAll]);
  18. end;
  19.  
  20. begin
  21.    S := Chr(39) + '7 setae' + Chr(39) + ' ' + Chr(39) + '9 setae' + Chr(39) + ' ' + Chr(39) + '10 setae' + Chr(39);
  22.    WriteLn(S);
  23.    S := ParseQuoted(S);
  24.   WriteLn(S);
  25. end.
  26.  

Thank you very much!

Best regards,
Title: Re: [SOLVED] Handling strings with single quotes
Post by: jamie on December 09, 2019, 11:08:17 pm
I would of done this

Code: Pascal  [Select][+][-]
  1. Const TestStr = '''One'''+' '+'''Two'''+' '+'''Three''';
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. Var
  4.   S:TStringList;
  5.   I:Integer;
  6. begin
  7.   S := TStringList.Create;
  8.   S.QuoteChar := ''''; //Set the quote character
  9.   S.Delimiter := ' ';  // set the separator
  10.   S.DelimitedText := TestStr; // A test string;
  11.   Memo1.Lines.Text := S.Text; // Test display..
  12.   For I := 0 To S.Count-1 do S[I] := Trim(S[i]); // Just in case there are spaces around it.
  13.   S.Free;
  14. end;                              
  15.  
Now the memo has all the single quoted items..
Title: Re: [SOLVED] Handling strings with single quotes
Post by: maurobio on December 09, 2019, 11:46:47 pm
@jamie,

Thanks!

Cheers,
TinyPortal © 2005-2018