Delphi:即使设置了分隔符,StringList分隔符始终是一个空格字符

编程入门 行业动态 更新时间:2024-10-25 18:23:23
本文介绍了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 。

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

  • 找到您知道的字符不会在原始文本(例如下划线)中使用,在分割之前将所有空格转换为该字符,并在分割后转换。这是 robosoft 的建议。
  • 如果没有反转逗号和文本中的空格,您可以使用亚历山大·的技巧,并在反向命令中将文本包围在分隔符之间,以便'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:

  • 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.
  • 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分隔符始终是一个空格字符

    本文发布于:2023-10-16 10:20:22,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1497292.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:是一个   分隔符   空格   字符   Delphi

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!