delphi有没有类似VBA里的Split函数 ( 积分: 20 )

  • 主题发起人 主题发起人 jskscxy
  • 开始时间 开始时间
分割字符串的吗?如果是我有代码的
 
谁说没有?delphi有啊,函数名是
ExtractStrings
比Split强N倍。
 
zqw0117兄:
救人救到底!
这个函数在那个单元,还有语法,怎么用。
谢谢!
我在VBA里是这样写的:
p=5,6,7,8,9,10,
ary = Split(Left(p, Len(p) - 1), ",")
 
终于有点明白了
现还有问题:
Split返回一个下标从零开始的一维数组
我都不知该如何说:就是将ExtractStrings返回的结果变成一维数组
 
Fills a string list with substrings parsed from a delimited list.
Unit
Classes
Category
string handling routines (null-terminated)
Delphi syntax:
function ExtractStrings(Separators, WhiteSpace: TSysCharSet;
Content: PChar;
Strings: TStrings): Integer;
C++ syntax:
extern PACKAGE int __fastcall ExtractStrings(TSysCharSet Separators, TSysCharSet WhiteSpace, char * Content, TStrings Strings);
Description
Use ExtractStrings to fill a string list with the substrings of the null-terminated string specified by Content.
Separators is a set of characters that are used as delimiters, separating the substrings. Carriage returns, newline characters, and quote characters (single or do
uble) are always treated as separators. Separators are ignored when inside a quoted string until the final end quote. (Note that quoted characters can appear in a quoted string if the quote character is do
ubled.)
WhiteSpace is a set of characters to be ignored when parsing Content if they occur at the begin
ning of a string.
Content is the null-terminated string to parse into substrings.
Strings is a string list to which all substrings parsed from Content are added. The string list is not cleared by ExtractStrings, so any strings already in the string list are preserved.
ExtractStrings returns the number of strings added to the Strings parameter.
Note: ExtractStrings do
es not add empty strings to the list.
 
function StrSplit(str: string;
tsStrs: TStrings;
SplitChar: Char;
bTrim: Boolean
= true): integer;
var
n, i, k: integer;
s: string;
begin
Result := 0;
if SplitChar = '' then
SplitChar := ',';
if tsStrs = nil then
Exit;
k := Length(str);
if (k = 0) then
Exit;
i := 1;
for n := 1 to k do
if str[n] = SplitChar then
begin
s := Copy(Str, i, n - i);
if bTrim then
s := Trim(s);
tsStrs.Add(s);
i := n + 1;
Inc(Result);
end;
if i <= k then
begin
s := Copy(Str, i, n - i);
if bTrim then
s := Trim(s);
tsStrs.Add(s);
Inc(Result);
end;
end;
 
多人接受答案了。
 
后退
顶部