Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

dcgeek

Do you know Delphi?

This topic is 5590 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If you do, please help me with this litle problem: I have a memo1, with this text: this is a string Now I want a function, which can copy all text between "this" and "string" (so I get "is a" because it is between "this" and "string"). It could be very nice, if I could make a function, so I can write something like this: var s : string; begin s := CopyFunction(''this'',''string'',memo1.text); end;

Share this post


Link to post
Share on other sites
Advertisement
Two functions of interest (maybe three): Pos, Copy and possibly Lowercase/Uppercase.

Pos returns the index of a substring within a string as an integer, or 0 if it''s not found. For example, Pos(''test'', ''a string test'') will return 10, since the first occurrence starts at the tenth character in the string (remember: strings in Delphi start at index 1, not 0). Bear in mind that pos is case sensitive.

Copy lets you copy substrings from within a string. The syntax is Copy(string_to_use, starting_index, number_of_chars_to_copy). For example, Copy(''a string test'', 10, 4) would return the word ''test'' as a separate string.

You might want case insensitive comparisons - if so, look at the Uppercase and Lowercase functions.

There are plenty of very highly optimised string libraries out there, which knock the default ones into a cocked hat. For example, have a look at FastStrings by Peter Morris. There''s also hyperstrings, but I can''t remember the URL and I''m not sure if it''s been updated to handle later Delphi versions (since it doesn''t come with source, just a .dcu). This article explains why FastStrings was made and the decisions behind it. It''s a pretty interesting read if you''re into that sort of thing.

It shouldn''t be too difficult to make your wanted function using a combination of Pos+Copy or FastStrings.

Share this post


Link to post
Share on other sites
Thank you very much

procedure TForm1.FormCreate(Sender: TObject);
var s,t : string; i,j,k,l : integer;
begin
i := pos(''this'',''blabla this is a string'')+length(''this'');
j := pos(''string'',''blabla this is a string'');
s := copy(''blabla this is a string'',i,j-+length(''string'')*2);

form1.caption := s;
end;

That''s the solution by using pos and copy

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!