-4

i have a problem developing this function, i have this text..

Testing Function
ok
US.Cool
rwgehtrhjyw54 US_Cool
fhknehq is ryhetjuy6u24
gflekhtrhissfhejyw54i

my function :

function TForm5.FindWordInString(sWordToFind, sTheString : String): Integer;
var
i : Integer; x:String;
begin
 Result := 0;
 for i:= 1 to Length(sTheString) do
   begin
    x := Copy(sTheString,i,Length(sWordToFind));
    if X = sWordToFind then
      begin
        if X.Length > sWordToFind.Length then
         begin
          Result := 100;
          break;
         end else

         begin
          Result := i;
          break;
         end;
       end;
   end;
end;

now, i want X to be US.Cool, but here its always = US, because i want to check the length of sWordToFind and X.

unknown
  • 65
  • 1
  • 2
  • 9
  • What's wrong with `SearchBuf` ([for example](https://pastebin.com/Ft6rcGy9))? – Victoria Nov 12 '17 at 20:35
  • well, it's great function, but i don't know how to modify it like i want in above, it gives True or False, like i search for `US` in `US.Cool` it would returns True, but then, i want to check the length of `US.Cool` where `US` were found. @Victoria – unknown Nov 12 '17 at 20:44
  • downvote ?! Classic – unknown Nov 12 '17 at 21:02
  • I'm a bit confused now. So you want to get length of the word (where string like `US.Cool` you consider as one word)? For example you'll search for `54` in your text and you want to get length of the first found word where the searched string is contained (length of `rwgehtrhjyw54`)? – Victoria Nov 12 '17 at 21:04
  • exactly, but only if 54 was at the beginning of the string @Victoria – unknown Nov 12 '17 at 21:07
  • Aha, so at the fourth line you would get 13 (length of `rwgehtrhjyw54`) when you searched for example for `rwgeh`. What if you searched on the same line for `US`? Would you get 7 (length of `US_Cool`)? And what you consider as word separators? – Victoria Nov 12 '17 at 21:11
  • yes, word separators = ' ' , or i'll take care of it later, the important thing now is to get the string length where sWordToFind were found. @Victoria – unknown Nov 12 '17 at 21:15
  • Try [this](https://pastebin.com/waS4HfAF). – Victoria Nov 12 '17 at 21:51
  • 4
    Stop explaining what you want in comments, and edit the question instead. We shouldn't have to play 20 questions in order to find out what you're asking. In a question and answer site, there are two jobs to be done. The first is yours, to **clearly explain the problem you're trying to solve and ask a clear question**. We can't do our poart (answer) until you've first done yours. – Ken White Nov 12 '17 at 22:11

2 Answers2

2

I spend a few times on your idea, so i wrote below codes, but it is not a good way for develop a Start With search. with some research you can find builtin functions, that provide better performance. you can try StrUtils.SearchBuf Function it will provide a full function string search.

anyway this code are working with SPACE separator, I hope it will be useful for you:

function TForm5.FindWordInString(sWordToFind, sTheString : String): Integer;
var
i : Integer; x:String;
flag : Boolean;
begin
 Result := 0;
 i := 1;
 flag := False;
 while True do
   begin
   if i > Length(sTheString) then  Break;

   if not flag then
       x := Copy(sTheString,i,Length(sWordToFind))
   else
   begin

       if sTheString[i] = ' ' then Break;
       x := x + sTheString[i];
   end;

    if (X = sWordToFind)  then
      begin
        flag := True;
        if (X.Length >= sWordToFind.Length) and
           (sTheString[i + Length(sWordToFind)]  = ' ') then
          break
        else
          i := i +  Length(sWordToFind) -1;

       end;


     i := i + 1;
   end;
   Result :=  Length(x);
end; 
Aqil
  • 360
  • 2
  • 16
  • @Victoria Of curse i know this is inefficient, but as I mentioned, I just write this base on the question owner idea,please read the descriptions before Codes, you will figure it out. – Aqil Nov 20 '17 at 23:59
  • I understood the point of this question; well, then what is the problem? Shall I review your code? I can do.. For start, stop copying so many times.. (just don't take it bad, I can teach you many things) – Victoria Nov 21 '17 at 00:20
  • I will really gratitude, that any one who teach me. Just say the way that I can learn from you Victoria. – Aqil Nov 21 '17 at 14:22
  • Please Edit it with your approach. – Aqil Nov 21 '17 at 14:26
  • It's perfectly fine to have alternative answers ;) – Victoria Nov 21 '17 at 14:28
2

After clarification, this question is about getting length of a word searched by its starting substring within a string. For example when having string like this:

fhknehq is ryhetjuy6u24

When you execute a desired function for the above string with the following substrings, you should get results like:

hknehq → 0 → substring is not at the beginning of a word
fhknehq → 7 → length of the word because substring is at the beginning of a word
yhetjuy6u24 → 0 → substring is not at the beginning of a word
ryhetjuy6u24 → 12 → length of the word because substring is at the beginning of a word

If that is so, I would do this:

function GetFoundWordLength(const Text, Word: string): Integer;
const
  Separators: TSysCharSet = [' '];
var
  RetPos: PChar;
begin
  Result := 0;
  { get the pointer to the char where the Word was found in Text }
  RetPos := StrPos(PChar(Text), PChar(Word));
  { if the Word was found in Text, and it was at the beginning of Text, or the preceding
    char is a defined word separator, we're at the beginning of the word; so let's count
    this word's length by iterating chars till the end of Text or until we reach defined
    separator }
  if Assigned(RetPos) and ((RetPos = PChar(Text)) or CharInSet((RetPos - 1)^, Separators)) then
    while not CharInSet(RetPos^, [#0] + Separators) do
    begin
      Inc(Result);
      Inc(RetPos);
    end;
end;
Victoria
  • 7,822
  • 2
  • 21
  • 44