Jump to content
Jeff Steinkamp

Split string on whitespace

Recommended Posts

Can someone point me in the right direction for splitting a string based on white space in a string.  In other words, the separator between elements could be 1 space, multiple spaces, or the tab character.

 

Thanks

Share this post


Link to post
1 hour ago, Jeff Steinkamp said:

In other words, the separator between elements could be 1 space, multiple spaces, or the tab character.

 

would be that?

 

procedure TForm1.Button1Click(Sender: TObject);
var
  LText  : string;
  LArrStr: TArray<string>;
begin
  LText := 'Hello World'#9' . .  Delphi in'#32'Action';
  // 2x spaces
  LArrStr := LText.Split(['  '  {, #9, #32}] { , TStringSplitOptions.ExcludeEmpty } );
  // Array with 2 items...  
  // 1x space
  LArrStr := LText.Split([' ', #9, #32] { , TStringSplitOptions.ExcludeEmpty } );
  // Array with 9 items...
  Memo1.Lines.AddStrings(LArrStr);
end;

 

Edited by programmerdelphi2k

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×