Pieter van Wyk
Pieter van Wyk

Reputation: 2378

Strip quotes from a string

How do I strip Quotes from a string using Delphi?

Ex. I need to remove all the quotes from 'A','B','C','D', giving a result of A,B,C,D. I've tried

MyVar := jclStrings.StrRemoveChars(sRegions, [#34]);

but no luck so far.

Thanks, Pieter

Upvotes: 8

Views: 20292

Answers (5)

Jon Lennart Aasenden
Jon Lennart Aasenden

Reputation: 4020

It's a fairly simple function to write. To strip all quotes (where multiple exists in a string), use StringReplace as demonstrated above. If you just want to un-quote a string, you can do this:

function UnQuote(Text: string): string; inline;
begin
  if ( Text.StartsWith('"') or Text.StartsWith('''') ) then
  begin
    if ( Text.EndsWith('"') or Text.EndsWith('''') ) then
      exit( copy(Text, 2, Text.Length-2) );
  end;
  result := Text;
end;

This simple routine takes for granted that both quote marks are there before it returns the content. You can smarten it up by doing a pos() search for the last quote-mark, but I will leave that up to you.

Upvotes: 1

Deltics
Deltics

Reputation: 23056

Although the other answers are all viable alternatives, the reason your specific code is not working is due to a simple mistake in the character code:

MyVar := jclStrings.StrRemoveChars(sRegions, [#34]);

The char code #34 represents the double quote char : "

What you need to remove are single quote/apostrophes: '

This has the character code #39, so this simple change should fix your original code:

MyVar := jclStrings.StrRemoveChars(sRegions, [#39]);

A simple way to avoid this sort of confusion is to use the literal char, rather than the char code (it will also make your code easier to read/understand later as you won't have to try to remember what char the code is supposed to represent - you could add a comment of course, but then that comment has to be kept up to date if you change the behaviour of the code itself... I personally prefer self documenting code as far as possible. But I digress).

Since the single quote char is used to delimit a char literal, an embedded single quote within a literal is represented as 2 consecutive single quotes:

MyVar := jclStrings.StrRemoveChars(sRegions, ['''']);

(NOTE: In Delphi 2010 I seem to recall that strings may now be delimited with either single or double quote chars, although I do not recall off-hand whether this extends to char literals (as distinct from single character strings. If so then this char literal could instead be expressed as "'", though whether you find this less or more confusing is a matter of personal preference. For myself I consider it less than sensible to mix string delimiters. Consistency is a great aid to accuracy in my experience.)

Upvotes: 10

Warren Markon
Warren Markon

Reputation: 201

You can use StrUtils.ReplaceText.

implementation

Uses StrUtils;

{$R *.dfm}

procedure TGeneric.Button1Click(Sender: TObject);
Var
  S: String;
begin
  S := '''A'',''B'',''C'',''D''';
  S := ReplaceText(S, '''', '');
  ShowMessage(S);
end;

S now equals 'A,B,C,D'.

ReplaceText will call AnsiReplaceText which in turn calls SysUtils.StringReplace with the replace flags [rfReplaceAll, rfIgnoreCase] set for you.

Upvotes: 7

ulrichb
ulrichb

Reputation: 20054

procedure TFormMain.Button1Click(Sender: TObject);
var
  s: String;
begin
  s := '''A'',''B'',''C'',''D''';
  s := StringReplace(s, '''', '', [rfReplaceAll]);
  /// now s contains 'A,B,C,D'
end;

Upvotes: 3

skamradt
skamradt

Reputation: 15548

You can use MyVar := StringReplace(MyVar,'''','',[rfReplaceAll]);

Upvotes: 14

Related Questions