delphi - How can find the first two word in string -


this question has answer here:

hy! best way find first 2 word in string? example, string adress : cross keys st 13. need 'cross keys' it. should count words in string or there better solution that?

i can first , last word easily. new in delphi. suggestions.

procedure sampleform.buttonclick(sender: tobject); var   st: string;   mystring : string;   c: integer; begin   st := cross keys st 13;   c:=lastdelimiter(' ',st);    mystring := copy(st,1,pos(' ',st)-1);   mystring:=copy(st,c+1,length(st)-c); 

the scope delphi xe string.split doesn't work. instead can use istringtokenizer httputil. this:

uses   httputil;  function getfirstnwrods(const str: string; const delim: string; numwords: integer): string; var   tokenizer: istringtokenizer; begin   result := '';   tokenizer := stringtokenizer(str, delim);   while (tokenizer.hasmoretokens) , (numwords > 0)   begin     result := result + tokenizer.nexttoken + delim;     dec(numwords)   end;    system.delete(result, length(result) - length(delim) + 1, length(delim)); end; 

example of how call function:

procedure tform1.formcreate(sender: tobject); begin   caption := getfirstnwrods('1 2 3 4', ' ', 2); end; 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -