This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
Idiom #49 Split a space-separated string
Build list chunks consisting in substrings of the string s, separated by one or more space characters.

- C
- Clojure
- C++
- C#
- D
- Dart
- Elixir
- Erlang
- Go
- Go
- Groovy
- Haskell
- JS
- Java
- Java
- Java
- Java
- Java
- Java
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- Rust
- Rust
- Scala
- Scheme
- Smalltalk
- VB
List<String> chunks = of(s.split(" +"));
Immutable
List<String> chunks = asList(s.split(" +"));
Immutable
Scanner t = new Scanner(s);
t.useDelimiter(compile(" +"));
List<String> chunks = t.tokens().toList();
t.close();
Scanner t = new Scanner(s);
t.useDelimiter(compile(" +"));
String chunks[] = t.tokens()
.toArray(String[]::new);
t.close();
List<String> chunks
= new ArrayList<>(of(s.split(" +")));
Mutable
$chunks = preg_split('/ +/', $s);
To match one ore more spaces, use preg_split instead of explode.
chunks = s.split()
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive ASCII whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the sequence has leading or trailing whitespace. Consequently, splitting an empty sequence or a sequence consisting solely of ASCII whitespace without a specified separator returns [].
(define (tokenize l)
(let loop ((t '())
(l l))
(if (pair? l)
(let ((c (car l)))
(if (char=? c #\space)
(cons (reverse t) (loop '() (cdr l)))
(loop (cons (car l) t) (cdr l))))
(if (null? t)
'()
(list (reverse t))))))
(define (string-split s)
(map list->string (tokenize (string->list s))))