nighTuner & Yuyu's Space

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  0 随笔 :: 35 文章 :: 0 评论 :: 0 Trackbacks

This article will help those that are trying to manipulate strings.  These examples are simple, but give you an idea of what can be accomplished.

First off, let's say that we have a string that we need to replace all the spaces into something.  We could use this:

StringReplace(Edit1.Text, ' ', '', [rfReplaceAll]);

This would go through Edit1.Text and replace all occurences of the space with nothing.  It has only been since Delphi 4 that we have received this function, but below is the code to use in earlier versions.

function StringReplace(const S, OldPattern, NewPattern: string;
 Flags: TReplaceFlags): string;
var
 SearchStr, Patt, NewStr: string;
 Offset: Integer;
begin
 if rfIgnoreCase in Flags then
 begin
   SearchStr := AnsiUpperCase(S);
   Patt := AnsiUpperCase(OldPattern);
 end else
 begin
   SearchStr := S;
   Patt := OldPattern;
 end;
 NewStr := S;
 Result := '';
 while SearchStr <> '' do
 begin
   Offset := AnsiPos(Patt, SearchStr);
   if Offset = 0 then
   begin
     Result := Result + NewStr;
     Break;
   end;
   Result := Result + Copy(NewStr, 1, Offset - 1) + NewPattern;
   NewStr := Copy(NewStr, Offset + Length(OldPattern), MaxInt);
   if not (rfReplaceAll in Flags) then
   begin
     Result := Result + NewStr;
     Break;
   end;
   SearchStr := Copy(SearchStr, Offset + Length(Patt), MaxInt);
 end;
end;

I have seen many people trying to get different parts of a string to use, like the end of an email address.

Let's take for example that we want to get the first part of support@delphipages.com.

First of all we want to use the Copy function to copy the desired area, and the Pos function to find out just when the ampersand appears in the line of text.

Edit2.text := Copy(Edit1.Text, 1, Pos('@', Edit1.Text)-1);

If we wanted the last part of the email:

Edit2.text := Copy(Edit1.Text, Pos('@', Edit1.Text)+1, Length(Edit1.Text)-Pos('@', Edit1.Text));

These two problems are fairly simple, but what if you had a string like this:

10,10,100,45,T,support@delphipages.com,80,douglas,tietjen

I could run through a series of Copy and Pos and Delete functions to be able to get the email address, but it would sure look hectic and hard to figure out problems.

I also develop with Cold Fusion, and I found some useful commands, and so I recreated them for me in Delphi, and others have done similiar things.

First off I wrote a function called ListGetAt.

function ListGetAt(List: String; Position: Integer; Delimiter: String = ','): String;

function TForm1.ListGetAt(List: String; Position: Integer;
 Delimiter: String): String;
var
  i, NP: integer;
begin
  NP := 0;
  for i := 1 to Position do begin
      List := Copy(List,NP,Length(List)-NP+1);
      NP := Pos(Delimiter,List)+Length(Delimiter);
      if i = Position then begin
          if Pos(Delimiter, List) = 0 then break;
          Delete(List, NP-Length(Delimiter), Length(List)-(NP-Length(Delimiter)-1));
      end;
  end;
  Result := List;
end;

Now, anytime that I need to get a section of code I can call this function for quick and easy access.  For example, now if I wanted the email address, I could write the following:

Edit2.Text := ListGetAt(Edit1.Text, 6, ',');

I would now have 'support@delphipages.com'.

If I just wanted the last part of the email, I could type this.

Edit2.Text := ListGetAt(ListGetAt(Edit1.Text, 6, ','), 2, '@');

Now suppose that the list of items were values that you wanted to change and store for later use.  It would be quite hectic again to go through and find each part and update it's value.  So I created another function called ListSetAt.

function ListSetAt(List: String; Position: Integer; Value: string; Delimiter: String = ','): String;

function TForm1.ListSetAt(List: String; Position: Integer; Value,
 Delimiter: String): String;
var
  i, NP: integer;
  BegStr, EndStr: string;
begin
 NP := 0;
 BegStr := '';
 EndStr := '';
 for i := 1 to Position do begin
   if i > 1 then
     BegStr := BegStr+Copy(List, 0, Pos(Delimiter,List)+Length(Delimiter)-1);
   List := Copy(List,NP,Length(List)-NP+Length(Delimiter));
   NP := Pos(Delimiter,List)+Length(Delimiter);
   if i = Position then begin
     if Pos(Delimiter, List) = 0 then break;
     EndStr := Copy(List, NP-Length(Delimiter), Length(List)-(NP-Length(Delimiter)-1));
   end;
 end;
 Result := BegStr+Value+EndStr;
end;

Now if I was to update the email address, I could just type this.

Edit1.Text := ListSetAt(Edit1.Text, 6, 'email@email.com', ',');

It would now update just the sixth position.  Now you could use it all sorts of ways.  If I just wanted everything after the ampersand, then I could type this

ListGetAt(Edit1.Text, 2, '@');

It would give the result of: delphipages.com,80,douglas,tietjen

These functions are simple, for example, if you were to ListGetAt on Position 0 it would return the entire line back.  If you did it on Position 20, it would return the last item in the list.  In Cold Fusion they have a command called ListLen, so you can find out how many ListItems you are working with.


function ListLen(List: String; Delimiter: String=','): Integer;

function TForm1.ListLen(List: String; Delimiter: String): Integer;
begin
  Result := 1;
  While Pos(Delimiter, List) > 0 do begin
      Delete(List, 1, Pos(Delimiter, List)+Length(Delimiter)-1);
      Result := Result + 1;
  end;
end;

posted on 2005-04-14 01:54 nighTuner 阅读(157) 评论(0)  编辑  收藏 所属分类: Delphi

只有注册用户登录后才能发表评论。


网站导航: