unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, StdCtrls, ComCtrls;
Const Space=' ';
type
TForm1 = class(TForm)
RichEdit1: TRichEdit;
OpenDialog1: TOpenDialog;
Table1: TTable;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
Function Regulatestr(aString:string;Sepchar:string):string;
Function GetSubStr(var aString:string;SepChar:String):String;
Function GetSubStrNum(aString:string;SepChar:String):Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Function Tform1.RegulateStr(aString:String;Sepchar:String):String;
var
i,Num:Integer;
Flag:Boolean;
MyStr,TempStr:String;
begin
Flag:=False;//进行标志,去除多余的分割符
Num:=Length(aString);//计算aString串的长度
for i:=1 to Num do
begin
TempStr:=Copy (aString,i,1);//取aString串中的一字符
if TempStr<>SepChar then
begin
MyStr:=MyStr+Tempstr;
Flag:=True;
end
else
if(Flag=True)then
begin
Mystr:=Mystr+TempStr;
Flag:=False;
end;
end;
if MyStr[Length(MyStr)]<>SepChar then
MyStr:=MyStr+SepChar;
RegulateStr:=MyStr;
end;
Function Tform1.GetSubStr(var aString:String;SepChar:String):string;
var
Mystr:String;
SepCharPos:Integer;
begin
sepCharpos:=pos(sepchar,astring);//计算分割符在子串中的位置
MyStr:=Copy(aString,1,SepCharPos-1);//将分割符前所有字符放到mystr串中
Delete(aString,1,SepCharPos);//除去分割符和分割符前的子串
GetSubStr:=MyStr;//返回一个字段
end;
Function Tform1.GetSubStrnum(aString:String;SepChar:String):integer;
var
i:Integer;
StrLen:Integer;
Num:Integer;
begin
StrLen:=Length(aString);
Num:=0;
for i:=1 to StrLen do
if Copy(aString,i,1)=SepChar then
Num:=Num+1;
GetSubStrNum:=Num;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if opendialog1.execute then
richedit1.lines.loadfromfile(opendialog1.filename);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i,j:integer;
myline:string;
begin
with table1 do
begin
open;
for i:=0 to richedit1.lines.count-1 do
begin
myline:=regulatestr(richedit1.lines,space);
for j:=1 to getsubstrnum(myline,space) do
begin
append;
Fields[j-1].AsString:=getsubstr(myline,space);
post;
end;
end;
close;
end;
end;
end.
------------------------------------------------------------------------
当把数据添加到数据库中时,数据不会整排,而是一条一行??
学号 java 体育 品德 综合评测 C语言
1
1
1
1
1
1