一个简单的问题:如何操作文本字符串?

zx197093 2000-07-18 02:08:00
我新学DELPHI,遇到一个问题:如何操作TEDIT或TMEMO控件中的字符串。举例来说:
1、编辑框中有一段文字,如何从这堆文字中查找到某个关键字。
2、如何取出两个关键字中间的一行文字?
3、如何统计关键字一共出现了几次?
简单的问题,30分奉送!!!
...全文
184 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Hank 2000-07-19
  • 打赏
  • 举报
回复
Copy()函数是返回字符串从指定位置开始的指定个数的字符!
我那里的“D”是返回值!
蝈蝈俊 2000-07-19
  • 打赏
  • 举报
回复
如果用TRichEdit 更简单:

This example requires a TRichEdit, a TButton, and a TFindDialog.
Clicking the button click will display a Find Dialog to the right of the edit control. Filling in the "Find what" text and pressing the Find Next button will select the first matching string in the Rich Edit control that follows the previous selection.

procedure TForm1.Button1Click(Sender: TObject);

begin
FindDialog1.Position := Point(RichEdit1.Left + RichEdit1.Width, RichEdit1.Top);
FindDialog1.Execute;
end;

procedure TForm1.FindDialog1Find(Sender: TObject);
var
FoundAt: LongInt;
StartPos, ToEnd: Integer;
begin
with RichEdit1 do
begin
{ begin the search after the current selection if there is one }
{ otherwise, begin at the start of the text }
if SelLength <> 0 then

StartPos := SelStart + SelLength;
else

StartPos := 0;

{ ToEnd is the length from StartPos to the end of the text in the rich edit control }

ToEnd := Length(Text) - StartPos;

FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
if FoundAt <> -1 then
begin
SetFocus;
SelStart := FoundAt;
SelLength := Length(FindDialog1.FindText);
end;
end;
end;
zx197093 2000-07-19
  • 打赏
  • 举报
回复
请问DELPHI中难道没有专用的函数吗?这该是最常用的功能。
另外,Edit2.Text为D中的“D”是什么?是目标字符串吗?
谢谢!
JGTM2000 2000-07-19
  • 打赏
  • 举报
回复
简单的,pos函数可以得到子串中子字串的首次出现位置。SelStart/SelLength可以用来标示一段文字。Copy可以按位置得到字串的子串。统计关键字的出现次数用简单的循环加上pos函数就可以完成(这个算法效率不是很高,但是很容易懂):

function CountKeywords(AString, ASubString: string): cardinal;
var
p: cardinal;
begin
Result:=0;
p:=pos(ASubString, AString);
while p <> 0 do
begin
Inc(Result);
AString:=copy(AString,p+Length(ASubString),MAXINT);
p:=pos(ASubString, AString);
end;
end;

共参考。
Hank 2000-07-18
  • 打赏
  • 举报
回复
用Copy()函数,例如:
Edit1.Text:='DELPHI';
Edit2.Text:=Copy(Edit1.Text,1,1);

则Edit2.Text为D
其它的根据这个函数循环即可!

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧