Home get the whole word string after finding the word in a text

# get the whole word string after finding the word in a text

unknown
1#
unknown Published in 2017-11-12 20:07:53Z
 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.
Victoria
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; 
 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;