Delphi:即使设置了分隔符,StringList分隔符始终是一个空格字符 [英] Delphi: StringList Delimiter is always a space character even if Delimiter is set

查看:215
本文介绍了Delphi:即使设置了分隔符,StringList分隔符始终是一个空格字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TStringList类中遇到了分隔符问题。看看:

  var 
s:string;
sl:TStringList;

begin
sl:= TStringList.Create;
s:='Users ^ foo bar ^ bar foo ^ foobar ^ barfoo';
sl.Delimiter:='^';
sl.DelimitedText:= s;
ShowMessage(sl [1]);
结束

sl [1] 应该返回'foo bar'



sl [1] code>'foo'



似乎分隔符现在是'^' AND ''



任何想法?

解决方案

您应该为不被视为分隔符的空格设置 s1.StrictDelimiter:= True ,更多信息 here



由于您在不支持上述版本的版本中(如提交答案后所述),您有两个选项:


  1. 找到您知道的字符不会在原始文本(例如下划线)中使用,在分割之前将所有空格转换为该字符,并在分割后转换。这是 robosoft 的建议。

  2. 如果没有反转逗号和文本中的空格,您可以使用亚历山大·的技巧,并在反向命令中将文本包围在分隔符之间,以便'hello hello ^ bye bye'转为'hello hello^bye bye'。如果你选择这个路径,它适用于你,请接受亚历山大的答案,而不是我的答案,他还提供了执行代码。

两个解决方法都不使用 StrictDelimiter 有限制:第一个需要一些未使用的字符,第二个不需要原始文本中的反向逗号和空格。



也许现在是升级到更新版本的Delphi的时候了):


I am having trouble with the delimiter in the TStringList Class. Take a look:

var
  s: string;
  sl: TStringList;

begin
  sl := TStringList.Create;
  s := 'Users^foo bar^bar foo^foobar^barfoo';
  sl.Delimiter := '^';
  sl.DelimitedText := s;
  ShowMessage(sl[1]);
end;

sl[1] SHOULD return 'foo bar'

sl[1] DOES return 'foo'

It seems that the delimiter is now '^' AND ' '

Any ideas?

解决方案

You should set s1.StrictDelimiter := True for spaces not to be considered delimiters, more info here.

Since you work in a version that does not support the above (as was clarified after the answer was submitted), you have two options:

  1. Find a character you know will not be used in the original text (e.g. underscore), convert all spaces to that character before splitting, and convert back after splitting. This is robosoft's suggestion.
  2. If you don't have inverted commas and spaces in the text, you can use Alexander's trick and wrap the text between delimiters in inverted command, so that 'hello hello^bye bye' turns to '"hello hello"^"bye bye"'. If you do choose this path and it works for you, please accept Alexander's answer and not mine, he also provides the code to implement it.

Both workarounds not using StrictDelimiter have limitations: the first requires some unused character, and the second requires no inverted commas and spaces in the original text.

Maybe it's time to upgrade to a newer version of Delphi :)

这篇关于Delphi:即使设置了分隔符,StringList分隔符始终是一个空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆