Jump to content
LordJashin

How do you Split a string at the NewLine character?

Recommended Posts

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?

Link to comment
Share on other sites

@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 by Janilabo
Improvements
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...