根据分隔符将字符串拆分为字符串数组 [英] Split a string into an array of strings based on a delimiter

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

问题描述

我正在尝试找到一个 Delphi 函数,该函数将基于分隔符将输入字符串拆分为字符串数组.我在网上搜索了很多东西,但似乎每个人都有自己的问题,我无法让其中任何一个正常工作.

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 from searching the web, 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']. How can I do that?

推荐答案

您可以使用 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 的解释.

See this link for an explanation of StrictDelimiter.

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

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