string - How to get a specific field from delimited text -
i have string of delimited text ie: value1:value2:value3:value4:value5:value6
how extract, example, specific value ie: label.caption := getvaluefromdelimitedtext(2);
value2
thanks in advance
paul
this should it:
function getvaluefromdelimitedtext( const s: string; const separator: char; const index: integer ): string; var i, itemindex, start: integer; begin itemindex := 1; start := 1; := 1 length(s) begin if s[i]=separator begin if itemindex=index begin result := copy(s, start, i-start); exit; end; inc(itemindex); start := i+1; end; end; if itemindex=index begin result := copy(s, start, length(s)-start+1); end else begin result := ''; end; end;
this version allows specify separator, pass ':'
. if ask item beyond end function return empty string. change exception if preferred. finally, have arranged uses 1-based indexing per example, choose 0-based indexing.
Comments
Post a Comment