Almaz

Silver Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору STEEL Цитата: На входе есть строчка, которая разделена #10 символами | как обрабатывать два и более подряд разделителя? считать за один, или пустые строки между ними? антикварная rxLib rxStrUtils.pas Цитата: function WordCount(const S: string; const WordDelims: TCharSet): Integer; var SLen, I: Cardinal; begin Result := 0; I := 1; SLen := Length(S); while I <= SLen do begin while (I <= SLen) and (S[I] in WordDelims) do Inc(I); if I <= SLen then Inc(Result); while (I <= SLen) and not(S[I] in WordDelims) do Inc(I); end; end; | Цитата: function ExtractWord(N: Integer; const S: string; const WordDelims: TCharSet): string; var I: Integer; Len: Integer; begin Len := 0; I := WordPosition(N, S, WordDelims); if I <> 0 then { find the end of the current word } while (I <= Length(S)) and not(S[I] in WordDelims) do begin { add the I'th character to result } Inc(Len); SetLength(Result, Len); Result[Len] := S[I]; Inc(I); end; SetLength(Result, Len); end; | Цитата: // Функция аналогична функции ExtractWord. // В отличии от нее воспринимает 2 символа из Delims, идущих подряд, // как разделители между пустой строкой. // думается, что DelimitedCount (аналог WordCount) можно не приводить function ExtractDelimited(N: Integer; const S: string; const Delims: TCharSet): string; var CurWord: Integer; I, Len, SLen: Integer; begin CurWord := 0; I := 1; Len := 0; SLen := Length(S); SetLength(Result, 0); while (I <= SLen) and (CurWord <> N) do begin if S[I] in Delims then Inc(CurWord) else begin if CurWord = N - 1 then begin Inc(Len); SetLength(Result, Len); Result[Len] := S[I]; end; end; Inc(I); end; end; | |