Recent

Author Topic: [SOLVED] Handling strings with single quotes  (Read 473 times)

maurobio

  • Full Member
  • ***
  • Posts: 110
[SOLVED] Handling strings with single quotes
« 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,
« Last Edit: December 09, 2019, 10:06:50 pm by maurobio »
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7801
Re: Handling strings with single quotes
« Reply #1 on: December 09, 2019, 01:25:30 pm »
double the single quotes (one->two single quotes) inside the string

lucamar

  • Hero Member
  • *****
  • Posts: 2254
Re: Handling strings with single quotes
« Reply #2 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;
« Last Edit: December 09, 2019, 01:50:45 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

maurobio

  • Full Member
  • ***
  • Posts: 110
Re: Handling strings with single quotes
« Reply #3 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,
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home

MarkMLl

  • Hero Member
  • *****
  • Posts: 590
Re: Handling strings with single quotes
« Reply #4 on: December 09, 2019, 03:16:36 pm »
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

maurobio

  • Full Member
  • ***
  • Posts: 110
Re: Handling strings with single quotes
« Reply #5 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,
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home

lucamar

  • Hero Member
  • *****
  • Posts: 2254
Re: Handling strings with single quotes
« Reply #6 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;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

maurobio

  • Full Member
  • ***
  • Posts: 110
Re: Handling strings with single quotes
« Reply #7 on: December 09, 2019, 07:47:15 pm »
@lucamar,

Unfortunately, still no luck with TStringSplitOptions.ExcludeEmpty!

Best regards,
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home

lucamar

  • Hero Member
  • *****
  • Posts: 2254
Re: Handling strings with single quotes
« Reply #8 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;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.4/2.0.6  - FPC 3.0.4 on:
(K|L)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 709
Re: Handling strings with single quotes
« Reply #9 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

maurobio

  • Full Member
  • ***
  • Posts: 110
Re: Handling strings with single quotes
« Reply #10 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,
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home

jamie

  • Hero Member
  • *****
  • Posts: 2252
Re: [SOLVED] Handling strings with single quotes
« Reply #11 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..
Number 1 at blue screen app creations!

maurobio

  • Full Member
  • ***
  • Posts: 110
Re: [SOLVED] Handling strings with single quotes
« Reply #12 on: December 09, 2019, 11:46:47 pm »
@jamie,

Thanks!

Cheers,
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home