从字符串中去掉引号

发布于 2024-08-21 19:32:24 字数 199 浏览 4 评论 0原文

如何使用 Delphi 从字符串中去除引号?

前任。我需要删除 'A'、'B'、'C'、'D' 中的所有引号,得到 A、B、C、D 的结果。我已经尝试过

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

但到目前为止没有运气。

谢谢,彼得

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只有影子陪我不离不弃 2024-08-28 19:32:24

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

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

阳光下慵懒的猫 2024-08-28 19:32:24

尽管其他答案都是可行的替代方案,但您的特定代码不起作用的原因是由于字符代码中的一个简单错误:

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

字符代码#34代表双引号字符:“

您需要什么要删除的是单引号/撇号:'

这具有字符代码#39,因此这个简单的更改应该修复您的原始代码:

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

避免这种混乱的一个简单方法是使用文字字符,而不是 char 代码(它也将使您的代码稍后更容易阅读/理解,因为您不必尝试记住代码应该代表什么 char - 您当然可以添加注释,但随后该注释如果您更改代码本身的行为,请保持最新...我个人更喜欢尽可能自我记录代码,但我离题了),

因为单引号 char 用于分隔 char 文字,即嵌入式单引号 。文字中的引号表示为 2 个连续的单引号:(

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

注意: 在 Delphi 2010 中,我似乎记得字符串现在可以用单引号或双引号字符分隔,尽管我不这样做立即回忆一下这是否扩展到 char 文字(与单个字符串不同)。如果是这样,那么这个 char 文字可以表示为“'”,尽管您是否觉得这更容易或更令人困惑取决于个人喜好。就我自己而言,我认为混合字符串分隔符不太明智。根据我的经验,一致性对于提高准确性有很大帮助。

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.)

轻许诺言 2024-08-28 19:32:24

您可以使用 StrUtils.ReplaceText。

实现

使用StrUtils;

{$R *.dfm}

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

S 现在等于 'A,B,C,D'。

ReplaceText 将调用 AnsiReplaceText,AnsiReplaceText 又调用 SysUtils.StringReplace,并为您设置替换标志 [rfReplaceAll, rfIgnoreCase]。

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.

榆西 2024-08-28 19:32:24
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;
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;
小鸟爱天空丶 2024-08-28 19:32:24

这是一个编写起来相当简单的函数。要删除所有引号(字符串中存在多个引号),请使用 StringReplace,如上所示。
如果您只想取消对字符串的引用,可以这样做:

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;

这个简单的例程理所当然地认为在返回内容之前两个引号都存在。您可以通过 pos() 搜索最后一个引号来使其变得更智能,但我将把它留给您。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文