delphi中的字符串分割(100分)

  • 主题发起人 主题发起人 独孤
  • 开始时间 开始时间

独孤

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样把字符串按一定的标志值进行分割。如C和VB中的split函数
 
试试看TStrings的CommaText

摘自Delphi帮助文件

property CommaText: string;

Description

Use CommaText to get or set all the strings in the TStrings object in a single
comma-delimited string.

When retrieving CommaText, any string in the list that include spaces, commas
or quotes will be contained in double quotes, and any double quotes in
a string will be repeated. For example, if the list contains the following
strings:

Stri,ng 1

Stri"ng 2
String 3
String4

CommaText will return:

"Stri,ng 1","Stri""ng 2","String 3",String4

When assigning CommaText, the value is parsed as SDF formatted text.
For SDF format, strings are separated by commas or spaces, and optionally
enclosed in double quotes. Double quote marks that are part of the string
are repeated to distinguish them from the quotes that surround the string.
Spaces and commas that are not contained within double quote marks are delimiters.
Two commas next to each other will indicate an empty string, but spaces that
appear next to another delimiter are ignored. For example, suppose CommaText is
set to:

"Stri,ng 1", "Stri""ng 2" , String 3,String4

The list will then contain:

Stri,ng 1

Stri"ng 2
String
3
String4
 
看一看TStringList的SetCommaText(好像是这个)源码,稍稍修改一下就可以达到你的要求了。
 
好像不太好用。我有一个以前写的函数:
function SplitString(const source,ch:string):tstringlist;
var
temp:string;
i:integer;
begin
result:=tstringlist.Create;
temp:=source;
i:=pos(ch,source);
while i<>0 do
begin
result.Add(copy(temp,0,i-1));
delete(temp,1,i);
i:=pos(ch,temp);
end;
result.Add(temp);
end;
调用:
s:=splitstring('afsdfsdaaa|bbfdsfsdb|ccc','|');
for i:=0 to s.Count-1 do
b:=b+s.Strings+#13;
showmessage(b);
s.free;
 
到vcl.vclxx.org看看,有不少这方面的控件和Unit
 
Delphi6中的TStrings已经实现Delimiter和DelimiterText两个新的属性,利用他们可以
很容易的实现VB中的split函数的功能.其实现也不难,你可以把这两个属性的实现代码
拷贝至Delphi5,Delphi4中,注意把平台有关的编译指示字去掉就可以了.
下面是一个完整的实例:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
MyStrList : TStrings;
begin
MyStrList:=TStringList.Create;
MyStrList.Delimiter:=#32
//指定分隔符,此处为空格
MyStrList.DelimitedText:=Trim(Edit1.Text);
ListBox1.Items := MyStrList;
end;

end.


 
我早就想问了.谢谢大家了.
 
Classes 单元里面不是自带了这样一个函数吗,哪里用自己去写:

function ExtractStrings(Separators, WhiteSpace: TSysCharSet
Content: PChar;
Strings: TStrings): Integer;

其中:
Separators 就是分隔符集合,如 [',']
WhiteSpace 指前面可以忽略的字符集合,一般传空即可:[]
Content 就是待分割的字符啦
Strings 就是存放分割后各字符串的列表啊
 
用TStrings的Delimiter属性:

// Delphi 6 通过

procedure TForm1.Button1Click(Sender: TObject);
const
s = '111,222,333,444,555,666';
var
StrList: TStringList;
begin
StrList := TStringList.Create;
try
StrList.Delimiter := ',';
StrList.DelimitedText := s;
// 验证
Memo1.Lines.Assign(StrList);
finally
FreeAndNil(StrList);
end;
end;
 
{比如取"(世界杯),(中国队),(杨晨)"中括号中的字符串}
procedure TForm1.Button1Click(Sender: TObject);
var
s: array[0..255] of char;
strings: TStrings;
begin
strings:=TStringlist.Create;
s:='(世界杯),(中国队),(杨晨)';
ExtractStrings(['(',')'],[','],s,strings);
memo1.Lines.Clear
//这行可以去掉,只是为了显示
memo1.Lines.AddStrings(strings)
//这行可以去掉,只是为了显示
strings.Free;
end;
 
我想这是最简单的方法:
// 注意:S 是以变量参数形式的,具体我不懂表达,自己看吧
procedure SplitString(ch: char
var S: String
var xList: TList);
var
I: Integer
P: PChar;
begin
P:= @S[1]

for I:= 1 to Length(S) do
if S:= ch then begin
S:= #0

xList.Add(TObject(P));
P:= @S[I+1];
end;
end;
// 访问时应使用 PChar(xList[x]) 的方法

 
为何不结案配分?!
 
to PFan:
当我这样定义s时
s:='136454544,457871545,56456465';
分割函数是这样写的:
ExtractStrings([',',','],[ ],s,strings);
但编译同不过 错误是[Error] Unit1.pas(35): Incompatible types: 'Char' and 'String'
 
因为你的 ',' 是一个字符串而不是字符,它占用两个字节
你试一试这样:
var
t: string;
begin
t := ',';
ExtractStrings([',', t[1]], [t[2]],s,strings);
...
end;
 
后退
顶部