Recent

Author Topic: Escaping double quotes in strings?  (Read 6796 times)

py2pa

  • New Member
  • *
  • Posts: 11
Escaping double quotes in strings?
« on: August 18, 2014, 06:06:10 pm »
This code:

Code: [Select]
var
  separators: TSysCharSet;
  clue: PChar;
  words: TStringList;
begin
  separators := [' '];
  clue := '"Mary Poppins" is a musical film.';
  extractstrings(separators,separators,clue,words);

extracts the following five words from clue:

Code: [Select]
'"Mary Poppins"', 'is', 'a', 'musical', 'film'
while what I want is to somehow escape (but not omit) the double quotes and get the following six words:

Code: [Select]
'"Mary', 'Poppins"', 'is', 'a', 'musical', 'film'
Is this possible without writing a new function for the task?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Escaping double quotes in strings?
« Reply #1 on: August 18, 2014, 06:19:42 pm »
Maybe not with extractstrings, but you could try tstringlist, setting quotechar to something else. It then ignores quoting.

Then you can check if each word starts or ends with a quote and complete it where necessary.

py2pa

  • New Member
  • *
  • Posts: 11
Re: Escaping double quotes in strings?
« Reply #2 on: August 18, 2014, 07:17:12 pm »
Maybe not with extractstrings, but you could try tstringlist, setting quotechar to something else. It then ignores quoting.

Then you can check if each word starts or ends with a quote and complete it where necessary.

Following your lead, I rewrote the part as:

Code: [Select]
words.strictdelimiter := True;
words.delimiter := ' ';
words.quotechar := '^';
words.delimitedtext := clue;

and I just make sure the '^' doesn't show up in any clue.

Dzandaa

  • Full Member
  • ***
  • Posts: 249
  • From C# to Lazarus
Re: Escaping double quotes in strings?
« Reply #3 on: November 09, 2022, 04:37:10 pm »
Hi,

Why don't you use Strings instead PChar?

Code: Pascal  [Select][+][-]
  1. procedure TExperimentForm.BTestStringClick(Sender: TObject);
  2. var
  3.  Str: String;
  4.  SArr: TStringArray;
  5.  i: integer;
  6. begin
  7.   Str := '"Mary Poppins" is a musical film.';
  8.   SArr := Str.Split([' ']);
  9.  
  10.  
  11.   for i := 0 to Length(SArr) -1 do Memo.Append(SArr[i]);
  12.  
  13. end;
  14.  
  15.  

B->
Dzandaa

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Escaping double quotes in strings?
« Reply #4 on: November 09, 2022, 05:23:21 pm »
Nope:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}// make sure it is ansistring
  2. uses sysutils; // for the type helpers
  3. var
  4.  Str: String;
  5.  SArr: TStringArray;
  6. begin
  7.   Str := '"Mary Poppins" is a musical film.';
  8.   SArr := Str.Split([' ','"'], TStringSplitOptions.ExcludeEmpty); // options are qualified
  9.   for str in SArr do writeln(str); // no need for explicit index," i" had to go ;)
  10. end.
« Last Edit: November 09, 2022, 05:25:31 pm by Thaddy »
Specialize a type, not a var.

Dzandaa

  • Full Member
  • ***
  • Posts: 249
  • From C# to Lazarus
Re: Escaping double quotes in strings?
« Reply #5 on: November 09, 2022, 07:26:24 pm »
Hi,

You're right no need for index i (old c habit), but, Thaddy, I thing that py2pa want the "

Quote
while what I want is to somehow escape (but not omit) the double quotes and get the following six words:
'"Mary', 'Poppins"', 'is', 'a', 'musical', 'film'

 :D

B->
Dzandaa

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Escaping double quotes in strings?
« Reply #6 on: November 10, 2022, 09:36:44 am »
Just remove the second delimiter: '"'
Specialize a type, not a var.

CM630

  • Hero Member
  • *****
  • Posts: 1082
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Escaping double quotes in strings?
« Reply #7 on: November 28, 2022, 02:04:57 pm »
Nope:
Code: Pascal  [Select][+][-]
  1. ...
  2.   for str in SArr do writeln(str); // no need for explicit index," i" had to go
  3. end.
i seems fine to me, is not str a little too much?
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   SArr: TStringArray;
  4.   i:integer;
  5. begin
  6.   memo.Clear;
  7.   SArr := '"Mary Poppins" is a musical film.'.Split([' ','"'], TStringSplitOptions.ExcludeEmpty);
  8.   for i := 0 to Length(SArr) -1 do Memo.Append(SArr[i]);
  9. end;  
Лазар 3,2 32 bit (sometimes 64 bit); FPC3,2,2; rev: Lazarus_3_0 on Win10 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 14213
  • Probably until I exterminate Putin.
Re: Escaping double quotes in strings?
« Reply #8 on: November 28, 2022, 02:19:56 pm »
Nope. this code is safer and smaller:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2. uses sysutils;
  3. var
  4.   SArr: TStringArray;
  5.   s:string;
  6. begin
  7.   SArr := '"Mary Poppins" is a musical film.'.Split([' ','"'], TStringSplitOptions.ExcludeEmpty);
  8.   for s in Sarr do writeln(s);
  9. end.
Same can be done with a memo.
Specialize a type, not a var.

 

TinyPortal © 2005-2018