一个很简单的问题,如何实现字符截取功能…我很穷,只有50分… (50分)

  • 主题发起人 主题发起人 jamers
  • 开始时间 开始时间
J

jamers

Unregistered / Unconfirmed
GUEST, unregistred user!
[:D]
真的很简单,VB和一些其它程序都提供Mid函数,在delphi中我没有找到这样的函数,
应该可以实现的……
有人可以提供技术支持吧?
 
用pos函数,看帮助吧!
 
用copy()
例子:
var
s:string;
begin
s := '你是春意的朋友';
s := copy(s,5,4);
showmessage(s);
end;
 
TempPosition:=Pos(' ',str2);
Delete(str2,1,TempPosition);
TempPosition:=Pos(' ',str2);
ID:=Copy(str2,1,TempPosition-1);
// 电话号码
 
Returns a substring of a string or a segment of a dynamic array.
Unit
System
Category
string handling routines
function Copy(S;
Index, Count: Integer): string;
function Copy(S;
Index, Count: Integer): array;
Description
S is an expression of a string or dynamic-array type. Index and Count are integer-type expressions. Copy returns a substring or sub array containing Count characters or elements starting at S[Index].
If Index is larger than the length of S, Copy returns an empty string or array.
If Count specifies more characters or array elements than are available, only the characters or elements from S[Index] to the end of S are returned.
Note: When S is a dynamic array, Copy can only be used as a parameter in a call to a procedure or function that expects an array parameter. That is, it acts like the Slice function when working with dynamic arrays.


The following example shows how to complete partial strings typed into a combo box. The code represents the OnKeyPress event handler of the combo box, which performs most of the default keystroke handling before finding a matching list item and updating the text.
Note: This OnKeyPress event handler do
es not deal with the case when the user types the Delete key. That case must be caught in the OnKeyDown event handler instead.
procedure TForm1.ComboBox1KeyPress(Sender: TObject;
var Key: Char);
var
Found: boolean;
i,SelSt: Integer;
TmpStr: string;
begin
{ first, process the keystroke to obtain the current string }
{ This code requires all items in list to be uppercase}
if Key in ['a'..'z'] then
Dec(Key,32);
{Force Uppercase only!}
with (Sender as TComboBox) do
begin
SelSt := SelStart;
if (Key = Chr(vk_Back)) and (SelLength <> 0) then
TmpStr := Copy(Text,1,SelStart)+Copy(Text,SelLength+SelStart+1,255)
else
if Key = Chr(vk_Back) then
{SelLength = 0}
TmpStr := Copy(Text,1,SelStart-1)+Copy(Text,SelStart+1,255)
else
{Key in ['A'..'Z', etc]}
TmpStr := Copy(Text,1,SelStart)+Key+Copy(Text,SelLength+SelStart+1,255);
if TmpStr = '' then
Exit;
{ update SelSt to the current insertion point }
if (Key = Chr(vk_Back)) and (SelSt > 0) then
Dec(SelSt)
else
if Key <> Chr(vk_Back) then
Inc(SelSt);
Key := #0;
{ indicate that key was handled }
if SelSt = 0 then

begin
Text:= '';
Exit;
end;

{Now that TmpStr is the currently typed string, see if we can locate a match }
Found := False;
for i := 1 to Items.Count do
if Copy(Items[i-1],1,Length(TmpStr)) = TmpStr then
begin
Text := Items[i-1];
{ update to the match that was found }
ItemIndex := i-1;
Found := True;
Break;
end;
if Found then
{ select the untyped end of the string }
begin
SelStart := SelSt;
SelLength := Length(Text)-SelSt;
end
else
Beep;
end;
end;
 
谢谢了春意,
对于drc2100也同样表示感谢。
[:D]
 
不好意思,在我分配分数的时候,又有那么多朋友回帖!
我真的万分抱歉,分已经被我分完了,……
[:(]
 
后退
顶部