LordJashin Posted October 18, 2012 Share Posted October 18, 2012 I tried using TStringList. So what I'm trying to do is: [scar] program New; var str1: string; begin str1 := 'split' + #13#10 + 'text'; // Somehow split the string, so that I can get 'split' and 'text' by themselves // use the NewLine(#13#10) as a the point to split the string at end. [/scar] Is there a type I could cast the string to, so that I could do like...Pos function to find the #13#10 and split it at that point somehow? PChar maybe? Quote Link to comment Share on other sites More sharing options...
BryceTheCoder Posted October 18, 2012 Share Posted October 18, 2012 well what i can tell is your just declaring a simple string not a TStringList at all:/ Your just making the str1 equal to "split***text" ***what the hell does the #13#10 exactly mean? somehow ur not getting an error from not having that being a variable. Quote Link to comment Share on other sites More sharing options...
Janilabo Posted October 18, 2012 Share Posted October 18, 2012 (edited) @LJ: Are you looking for Explode()? Like: var i: Integer; str: string; TSA: TStrArray; begin str := 'Item 1' + #13#10 + 'Item 2' + #13#10 + 'Item 3'; TSA := Explode(#13#10, str); for i := 0 to 2 do WriteLn('TSA[' + IntToStr(i) + ']: "' + TSA[i] + '"'); SetLength(TSA, 0); end. ..and here is something I wrote: // Splits text using separator and returns a string with ID from the splitted text. function Split(text, separator: string; ID: Integer): string; var tmp: TStrArray; begin if (ID > -1) then if (text <> '') then begin tmp := ExplodeEx(separator, text, (ID + 2)); if ((ID < (High(tmp) + 1)) and (ID > -1))then Result := tmp[iD]; SetLength(tmp, 0); end; end; var str: string; begin ClearDebug; str := 'This is a test for Split() function...' #13#10'So lets see, if it works?' #13#10'Split() WORKS! ' // We will get this, with ID "2". #13#10'This is the end!'; WriteLn(Split(str, #13#10, 2)); end. Also, SplitEx(): // Splits text using separator and returns strings with IDs from the splitted text. function SplitEx(text, separator: string; IDs: TIntArray): TStrArray; var h, i, l: Integer; tmp: TStrArray; begin l := Length(IDs); if (l < 1) then Exit; SetLength(Result, l); if (text <> '') then begin tmp := ExplodeEx(separator, text, (TIAMax(IDs) + 2)); h := High(tmp); for i := 0 to (l - 1) do if ((IDs[i] < (h + 1)) and (IDs[i] > -1)) then Result[i] := tmp[iDs[i]]; SetLength(tmp, 0); end; end; var h, i: Integer; str: string; TSA: TStrArray; begin ClearDebug; str := 'Item 1' + #13#10 + 'Item 2' + #13#10 + 'Item 3' + #13#10 + 'Item 4' + #13#10 + 'Item 5' + #13#10 + 'Item 6' + #13#10 + 'Item 7'; TSA := SplitEx(str, #13#10, [0, 2, 4, 6]); h := High(TSA); for i := 0 to h do WriteLn(TSA[i]); SetLength(TSA, 0); end. @Epix: #13#10 is basically new line.. Take a look at HERE. -Jani Edited October 18, 2012 by Janilabo Improvements Quote Link to comment Share on other sites More sharing options...
LordJashin Posted October 18, 2012 Author Share Posted October 18, 2012 Perfect! Those might even be functions I could add to OSI too! Splendid! Now I can finish the logging stuff I've been working on! (Made it so there is always 1 WriteLn and then uses #13#10 for more lines)! Quote Link to comment Share on other sites More sharing options...