Recent

Author Topic: String replace or add ' not "  (Read 858 times)

eldonfsr

  • Hero Member
  • *****
  • Posts: 532
String replace or add ' not "
« on: January 15, 2025, 10:23:59 pm »
I tried to replace or add double quotes to single quote but i don't get is way to replace
what i tried to do is validate register on table so need to send  keyfields but whe i made locate i need separate them for fieldname and fieldvalue..

Code: Pascal  [Select][+][-]
  1. Procedure TForMMain.MigrateTable(OldTbl:TBufDataSet; NewTbl:TBufDataSet; KeyField:Array of String );
  2. Var Fldname:String;
  3.     cf,i:Integer;
  4.  begin
  5.  if( NewTbl.Active = false) then begin
  6.     NewTbl.Open;
  7.  end;
  8.  oldtbl.First;
  9.  while not oldtbl.EOF do begin
  10.    if( length(KeyField)=1) then begin
  11.       Fldname:= ''''+KeyField[0]+'''';
  12.       if( newtbl.Locate(FldName, Oldtbl.FieldByName(FldName).asString , [])) then begin
  13.         newtbl.edit;
  14.       end else begin
  15.         newtbl.Append;
  16.       end;
  17.    end else begin
  18.      Fldname:= ''+KeyField[0];
  19.      for i:=1 to length(KeyField)-1 do begin
  20.        Fldname:= Fldname+chr(39)+','+chr(39)+KeyField[i]+chr(39);
  21.      end;
  22.  
  23. //   show fldname but its contain " not single quote ans lazarus need..
  24.  
  25.  
  26.      if( newtbl.Locate(FldName, Arrayof(Oldtbl.FieldByName(KeyField[0]).asString+ , [])) then begin
  27.        newtbl.edit;
  28.      end else begin
  29.        newtbl.Append;
  30.      end;
  31.    end;
  32.    newtbl.Append;
  33.    for cf:=0 to oldtbl.FieldCount-1 do begin
  34.       fldname:= oldtbl.FieldDefs[cf].DisplayName;
  35.       newtbl.FieldByName(fldname).Value:= oldtbl.FieldByName(fldname).Value;
  36.    end;
  37.    newtbl.Post;
  38.    oldtbl.Next;
  39.  end;
  40.  NewTbl.SaveToFile(oldtbl.FileName);
  41. end;                                  
  42. code]

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: String replace or add ' not "
« Reply #1 on: January 16, 2025, 06:14:41 am »
Something like this? (untested)
Code: Pascal  [Select][+][-]
  1. {$mode objfpc){$H+}
  2. uses sysutils;
  3. var
  4.   s:string= 'test"me"a bit "more" or "something"';
  5. begin
  6.   s:= stringreplace(s,#34,#39,[rfReplaceAll]);
  7. end.
#39 avoids ''''
« Last Edit: January 16, 2025, 06:26:27 am by Thaddy »
But I am sure they don't want the Trumps back...

eldonfsr

  • Hero Member
  • *****
  • Posts: 532
Re: String replace or add ' not "
« Reply #2 on: January 16, 2025, 03:18:17 pm »
Yes but #34 and #39 is same both are " and i need '

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1460
    • Lebeau Software
Re: String replace or add ' not "
« Reply #3 on: January 16, 2025, 04:49:23 pm »
Yes but #34 and #39 is same both are " and i need '

No, #34 is " (double quote) and #39 is ' (single quote).

You could also use:

Code: Pascal  [Select][+][-]
  1. s := StringReplace(s, '"', '''', [rfReplaceAll]);

But, if this is not accomplishing what you need, then you need to explain better what you are actually trying to do.

Also, have a look at QuotedStr() or TStringHelper.QuotedString() or TStringList.DelimitedText.
« Last Edit: January 16, 2025, 04:54:29 pm by Remy Lebeau »
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: String replace or add ' not "
« Reply #4 on: January 16, 2025, 05:09:22 pm »
Tnx for pointing that out, Remy.
(the # notation saves you from using '''' etc)
« Last Edit: January 16, 2025, 05:14:17 pm by Thaddy »
But I am sure they don't want the Trumps back...

 

TinyPortal © 2005-2018