Search code examples
smalltalk

How do I split a string using Smalltalk using space as the delimiter


I have string like "Hello World". I need to split this string to Hello, World`` using space delimiter in Smalltalk.

In Java the code looks as follows

 String message = "Hello world"
 message.split(' ');

How do I do this in Smalltalk?


Solution

  • Closer to Java:

    'Hello World' splitOn: Character space.
    

    also works with:

    'Hello World' splitOn: ' '.
    

    or (more funky):

    [ :each | each isSeparator  ] split: 'Hello World'.
    
    'Hello World' splitOn: [ :each | each isSeparator  ].