简体   繁体   English

从字符串中去除引号

[英]Strip quotes from a string

How do I strip Quotes from a string using Delphi?如何使用 Delphi 从字符串中删除引号?

Ex.前任。 I need to remove all the quotes from 'A','B','C','D', giving a result of A,B,C,D.我需要从“A”、“B”、“C”、“D”中删除所有引号,得到 A、B、C、D 的结果。 I've tried我试过了

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

but no luck so far.但到目前为止还没有运气。

Thanks, Pieter谢谢,彼得

您可以使用MyVar := StringReplace(MyVar,'''','',[rfReplaceAll]);

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 : "字符代码#34 表示双引号字符:"

What you need to remove are single quote /apostrophes: '您需要删除的是单引号/撇号:'

This has the character code #39, so this simple change should fix your original code:它的字符代码为 #39,因此这个简单的更改应该可以修复您的原始代码:

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:由于单引号字符用于分隔字符文字,文字中嵌入的单引号表示为 2 个连续的单引号:

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. ) 注意:在 Delphi 2010 中,我似乎记得字符串现在可以用单引号或双引号字符分隔,尽管我不记得这是否扩展到字符文字(与单字符串不同。如果是这样,那么这个char 文字可以表示为“'”,尽管您是否觉得这或多或少令人困惑是个人喜好的问题。就我个人而言,我认为混合字符串分隔符不太明智。在我的经​​验中,一致性对准确性有很大帮助. )

You can use StrUtils.ReplaceText.您可以使用 StrUtils.ReplaceText。

implementation执行

Uses StrUtils;使用 StrUtils;

{$R *.dfm} {$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'. S 现在等于“A、B、C、D”。

ReplaceText will call AnsiReplaceText which in turn calls SysUtils.StringReplace with the replace flags [rfReplaceAll, rfIgnoreCase] set for you. ReplaceText 将调用 AnsiReplaceText,后者又调用 SysUtils.StringReplace 并为您设置替换标志 [rfReplaceAll, rfIgnoreCase]。

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;

It's a fairly simple function to write.这是一个相当简单的函数编写。 To strip all quotes (where multiple exists in a string), use StringReplace as demonstrated above.要去除所有引号(字符串中存在多个引号),请使用 StringReplace,如上所示。 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.您可以通过对最后一个引号进行 pos() 搜索来使其更智能,但我将把它留给您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM