基于分隔符将字符串拆分成字符串数组 [英] Split a string into an array of strings based on a delimiter

查看:309
本文介绍了基于分隔符将字符串拆分成字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一个Delphi函数,它将一个输入字符串分割成一个基于分隔符的字符串数组。我在Google上找到了很多,但似乎都有自己的问题,我无法让任何人上班。

I'm trying to find a Delphi function that will split an input string into an array of strings based on a delimiter. I've found a lot on Google, but all seem to have their own issues and I haven't been able to get any of them to work.

我只需要要将一个字符串分为:
word:doc,txt,docx转换为基于':'的数组。结果将是
['word','doc,txt,docx']

I just need to split a string like: "word:doc,txt,docx" into an array based on ':'. The result would be ['word', 'doc,txt,docx'].

有没有人有他们知道的功能吗?

Does anyone have a function that they know works?

谢谢

推荐答案

p>您可以使用TStrings.DelimitedText属性来分割一个字符串

you can use the TStrings.DelimitedText property for split an string

检查此示例

program Project28;

{$APPTYPE CONSOLE}

uses
  Classes,
  SysUtils;

procedure Split(Delimiter: Char; Str: string; ListOfStrings: TStrings) ;
begin
   ListOfStrings.Clear;
   ListOfStrings.Delimiter       := Delimiter;
   ListOfStrings.StrictDelimiter := True; // Requires D2006 or newer.
   ListOfStrings.DelimitedText   := Str;
end;


var
   OutPutList: TStringList;
begin
   OutPutList := TStringList.Create;
   try
     Split(':', 'word:doc,txt,docx', OutPutList) ;
     Writeln(OutPutList.Text);
     Readln;
   finally
     OutPutList.Free;
   end;
end.

更新

看到这个链接,以供解释 StrictDelimiter

这篇关于基于分隔符将字符串拆分成字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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