求助!通过字符串中的分界符,把一列分为多列!
有 表1(CNO,MESSAGE)
CNO MESSAGE
-----------------------------------------
001 2021|AA|20070902
002 2022|CC|20070905
003 2058|BB|20070906
在 Edit1.text输入 "001", 点击BitBtn1后 ,则在DBGridEh1显示如下
CNOC1c2 C3
-----------------------------------------
0012021AA20070902
通过分界符"|" 把MESSAGE字符串 “2021|AA|20070902“ 分为3列
在Edit1.text再次输入“002“ ,点击BitBtn1后 DBGridEh1 结果如下
CNOC1c2C3
---------------------------------
0012021AA20070902
0022022CC20070905
只有当点击BitBtn2后,才将DBGridEh1的结果集 插入到
表2(CNO,C1,c2,C3)
数据库是sybase,新手发帖,帮忙回答一下,谢了!
[解决办法]
with TStringList.Create do
try
CommaText := StringReplace(S, '', ',', [rfReplaceAll]);
....
finally
Free;
end;
[解决办法]
- Delphi(Pascal) code
uses IdStrings;var s : string; sl : TStringList; i : Integer;begin s := 'asd 3243|435 456346|4364654'; sl :=TStringList.Create; SplitColumns(s,sl,'|'); for i:=0 to sl.Count-1 do ShowMessage(sl.Strings[i]); sl.Free;end;
[解决办法]
SplitColumns是自定义函数吧,楼上的代码没贴全
[解决办法]
- Delphi(Pascal) code
实现大概也就是这样var stringList:TStringList; IntI:Integer; procedure SplitColumns(subStr:string;s:string;var strList:TStringList); var Index:Integer; begin Index:=Pos( subStr, s ); while Index > 0 do begin strList.Add( Trim( Copy( s, 1, Index-1 ) ) ); s:=copy( s, Index+1, length( s ) - Index ); Index:=Pos( subStr, s ); end; strList.Add( Trim( s ) ); end;begin stringList:= TStringList.Create; SplitColumns( '&','2021 ¦AA ¦20070902',stringList ); self.ListBox1.Items.Assign( stringList ); stringList.Free;end;
[解决办法]
我不会用Sybase,SQL函数如下(自己转化为Sybase):
CREATE function GetIndexStr(@s varchar(30),@Idx int,@c char) returns varchar(30)
as
begin
declare @strTmp varchar(30)
if (@Idx <= 0) or (CharIndex(@c,@s) = 0)
set @strTmp = @s
else if @Idx = 1
set @strTmp = SubString(@s,1,CharIndex(@c,@s)-1)
else
set @strTmp = GetIndexStr(SubString(@s,CharIndex(@c,@s)+1,len(@s)),@Idx-1,@c)
return @strTmp
end
然后再用SQL语句得出数据集
select CNO,GetIndexStr(Message,1,'¦') C1,GetIndexStr(Message,2,'¦') C2,
GetIndexStr(Message,3,'¦') C3 from 表1 where CNO <= ''' + Edit1.text +'''
[解决办法]
SplitColumns是IdStrings.pas里的函数,
它修正了TStrings.DelimitedText和TStrings.Delimiter将空格也作为分隔符的问题
IdStrings.pas是Indy提供的一个字符单元,D6以上版本自带