LordJashin Posted October 20, 2012 Share Posted October 20, 2012 So basically I am copying from the string from pos 1 to pos 2, to get everything after the last #10 or #13 to before the next #10 or #13. Then do that for each one. Then go back and delete any emptys in the array. [scar] {=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= function StrToArrByLineBreaks(s: string): TStrArray; Contributors: LordJashin Description: Returns TStrArray from string by #13 or #10 line breaks that the string already has. Note it checks for #13 and #10 separately then filters out blanks then takes the array that has the highest length. Date Created: October 20th, 2012. By LordJashin. Last Modified: October 20th, 2012. By LordJashin. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=} function StrToArrByLineBreaks(s: string): TStrArray; var TSA: TStrArray; TIA: TIntArray; str: string; i, j, k: Integer; begin Result := TSA; if (s = '') then Exit; k := 1; while (PosEx(#10, s, k) > 1) or (PosEx(#13, s, k) > 1) do begin SetLength(TSA, Length(TSA) + 1); i := PosEx(#10, s, k); j := PosEx(#13, s, k); if ((i < j) or (j = 0)) and (i <> 0) then begin TSA[High(TSA)] := StrCopy(s, k, i - 1); k := i + 1; end; if ((j < i) or (i = 0)) and (j <> 0) then begin TSA[High(TSA)] := StrCopy(s, k, j - 1); k := j + 1; end; end; for i := 0 to High(TSA) do begin str := TSA; while StrInStr(' ', str) do str := Replace(str, ' ', ''); if (str = '') then begin SetLength(TIA, Length(TIA) + 1); TIA[High(TIA)] := i; end; end; for i := 0 to High(TIA) do Delete(TSA, TIA - i, 1); Result := TSA; end; [/scar] Quote Link to comment Share on other sites More sharing options...
Janilabo Posted October 20, 2012 Share Posted October 20, 2012 (edited) Here mate: function StrReplaceMulti2(Text: string; findStrArr: TStrArray; replaceStr: string): string; var h, e, i, l, m, o, p: Integer; begin h := High(findStrArr); Result := Text; if ((Result <> '') and (h > -1)) then begin o := 1; e := Length(replaceStr); repeat l := 0; for i := 0 to h do begin p := PosEx(findStrArr[i], Result, o); case (p < 1) of True: begin Delete(findStrArr, i, 1); Dec(i); Dec(h); end; False: if ((l = 0) or (p < l)) then begin m := i; l := p; end; end; end; if (l > 0) then begin Delete(Result, l, Length(findStrArr[m])); Insert(replaceStr, Result, l); o := (l + e); end; until (l <= 0); end; end; procedure TSARemoveEmptyItems(var TSA: TStrArray); var h, i: Integer; begin h := High(TSA); for i := h downto 0 do if (Trim(TSA[i]) = '') then Delete(TSA, i, 1); end; var str: string; h, i: Integer; TSA: TStrArray; begin ClearDebug; str := 'Test1' #10'Test2' #13'Test3' #13#10'Test4' #13'Test5' #13#10'Test6' #10'Test7' #13' ' #10'Test8' #13#10 #13#10 #10'Test9' #13' ' #13#10'Test10'; str := StrReplaceMulti2(str, [#13#10, #13, #10], #13#10); TSA := Explode(#13#10, str); TSARemoveEmptyItems(TSA); h := High(TSA); for i := 0 to h do WriteLn('TSA[' + IntToStr(i) + ']: "' + TSA[i] + '"'); end. Is that what you are looking for? Edit: ..or maybe you are looking for something like this: function StrReplaceMulti2(Text: string; findStrArr: TStrArray; replaceStr: string): string; var h, e, i, l, m, o, p: Integer; begin h := High(findStrArr); Result := Text; if ((Result <> '') and (h > -1)) then begin o := 1; e := Length(replaceStr); repeat l := 0; for i := 0 to h do begin p := PosEx(findStrArr[i], Result, o); case (p < 1) of True: begin Delete(findStrArr, i, 1); Dec(i); Dec(h); end; False: if ((l = 0) or (p < l)) then begin m := i; l := p; end; end; end; if (l > 0) then begin Delete(Result, l, Length(findStrArr[m])); Insert(replaceStr, Result, l); o := (l + e); end; until (l <= 0); end; end; function TSATrim(const TSA: TStrArray; delEmpty: Boolean): TStrArray; var l, i, r: Integer; tmp: TStrArray; begin tmp := TSA; l := Length(tmp); SetLength(Result, l); if (l > 0) then if delEmpty then begin for i := 0 to (l - 1) do if (tmp[i] <> '') then begin Result[r] := Trim(tmp[i]); if (Result[r] <> '') then Inc(r); end; SetLength(Result, r); end else for i := 0 to (l - 1) do Result[i] := Trim(tmp[i]); SetLength(tmp, 0); end; var str: string; h, i: Integer; TSA, TSA2: TStrArray; begin ClearDebug; str := 'Test1' #10' Test2 ' #13'Test3 ' #13#10' Test4 ' #13' Test5 ' #13#10'Test6' #10'Test7 ' #13' ' #10' Test8' #13#10 #13#10 #10'Test9 ' #13' ' #13#10' Test10'; str := PregReplace('/[ \t]{1,}/', ' ', str); // Replaces all 2+ empty spaces with just 1 space. str := StrReplaceMulti2(str, [#13#10, #13, #10], #13#10); TSA2 := Explode(#13#10, str); TSA := TSATrim(TSA2, True); SetLength(TSA2, 0); h := High(TSA); for i := 0 to h do WriteLn('TSA[' + IntToStr(i) + ']: "' + TSA[i] + '"'); SetLength(TSA, 0); end. -Jani Edited October 20, 2012 by Janilabo Quote Link to comment Share on other sites More sharing options...
LordJashin Posted October 20, 2012 Author Share Posted October 20, 2012 Nice, I just must've lost my brain last night or whenever I did that thing. If I had thought about it more, I could've did like you did in the first example. Just replace any variation of #13#10 with #13#10 Thanks Jani. Now I can continue with the logging system, and hopefully this turns out good. Hopefully I can get this out by tonight. Quote Link to comment Share on other sites More sharing options...
Janilabo Posted October 20, 2012 Share Posted October 20, 2012 No problem mate, glad it helped you out! Quote Link to comment Share on other sites More sharing options...
BryceTheCoder Posted October 21, 2012 Share Posted October 21, 2012 haha you know what i just relized, LordJashin, on your 1st post your line numbers are sooo wrong hahaha:P Quote Link to comment Share on other sites More sharing options...
LordJashin Posted October 21, 2012 Author Share Posted October 21, 2012 Where? Quote Link to comment Share on other sites More sharing options...
Janilabo Posted October 21, 2012 Share Posted October 21, 2012 Hey LJ, here is something that you might like.. "StrExplodeMulti()": // Explodes str with multiple separators/delimiters (d). // The importance order for d items is from left to right (=>). // So place the important ones first and then less important after those. function StrExplodeMulti(d: TStrArray; str: string): TStrArray; var p, h, i, x, o, m, l: Integer; begin h := High(d); if (h > -1) then begin o := 1; SetLength(Result, Length(str)); repeat l := 0; for x := 0 to h do begin p := Pos(d[x], str); case (p < 1) of True: begin Delete(d, x, 1); Dec(x); Dec(h); end; False: if ((l = 0) or (p < l)) then begin m := x; l := p; end; end; end; if (l > 0) then begin Result[i] := Copy(str, 1, (l - 1)); Delete(str, 1, ((l + Length(d[m])) - 1)); Inc(i); end else Result[i] := Copy(str, 1, Length(str)); until (l = 0); SetLength(Result, (i + 1)); end else Result := [str]; end; var str: string; h, i: Integer; TSA: TStrArray; begin str := 'Test1' #10'Test2' #13'Test3' #13#10'Test4' #13'Test5' #13#10'Test6' #10'Test7' #13'Test8' #10'Test9' #13#10'Test10'; TSA := StrExplodeMulti([#13#10, #13, #10], str); h := High(TSA); for i := 0 to h do WriteLn('TSA[' + IntToStr(i) + ']: "' + TSA[i] + '"'); SetLength(TSA, 0); end. Included an example for it Quote Link to comment Share on other sites More sharing options...
LordJashin Posted October 21, 2012 Author Share Posted October 21, 2012 Amazingggggg. But just for clarification: Wouldn't we want to change this: [scar] TSA := StrExplodeMulti([#13#10, #13, #10], str); [/scar] To this: [scar] TSA := StrExplodeMulti([#13#10, #10#13, #13, #10], str); [/scar] So then that would cover all possible variations? (added #10#13) So how does this work exactly? This is what I'm talking about...how am I supposed to figure this whole thing out D: it looks like speedymgee though. I know what it does, just looking at the source for that function makes me go AWAL|> [scar] h := High(d); if (h > -1) then begin o := 1; SetLength(Result, Length(str)); repeat l := 0; for x := 0 to h do begin p := Pos(d[x], str); case (p < 1) of True: begin Delete(d, x, 1); Dec(x); Dec(h); end; False: if ((l = 0) or (p < l)) then begin m := x; l := p; end; end; end; if (l > 0) then begin Result := Copy(str, 1, (l - 1)); Delete(str, 1, ((l + Length(d[m])) - 1)); Inc(i); end else Result := Copy(str, 1, Length(str)); until (l = 0); SetLength(Result, (i + 1)); end else Result := [str]; [/scar] Quote Link to comment Share on other sites More sharing options...
Janilabo Posted October 21, 2012 Share Posted October 21, 2012 Yeah you are correct, that would cover em all! Quote Link to comment Share on other sites More sharing options...
LordJashin Posted October 21, 2012 Author Share Posted October 21, 2012 (edited) I guess most of it is logic on how things work, and combining it with speed, and smallness. Watch this space though, I will pull a Jani, and add like 3 functions to show how stupid LordJashin is when it comes to my awesomeness with these things Jk. EDIT: WOOT i am at my 602th post ! Edited October 21, 2012 by LordJashin Quote Link to comment Share on other sites More sharing options...