Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #49 Split a space-separated string

Build list chunks consisting in substrings of the string s, separated by one or more space characters.

Turning the string "what a mess" into the 3 strings "what", "a", "mess"
import std.range;
auto chunks = s.splitter;
#include <string.h>
chunks[0] = strtok(s, " ");
for (int i = 1; i < N; ++i)
{
    chunks[i] = strtok(NULL, " ");
    
    if (!chunks[i])
        break;
}
(require '[clojure.string :refer [split]])
(def chunks (split s #"\s+"))
#include <sstream>
#include <algorithm>
#include <iterator>
#include <list>
std::istringstream x(s);
std::list<std::string> chunks;
std::copy(std::istream_iterator<std::string>(x), std::istream_iterator<std::string>(), std::back_inserter(chunks));
string[] chunks = s.Split(' ');
s.split(new RegExp('\\s+'))
chunks = String.split(s)
Chunks = string:tokens(S, [$\s]).
import "strings"
chunks := strings.Split(s, " ")
import "strings"
chunks := strings.Fields(s)
def chunks = s.split(/\s+/)
chunks = words s
let chunks = s.split(/ +/);
import static java.util.List.of;
import java.util.ArrayList;
import java.util.List;
List<String> chunks
    = new ArrayList<>(of(s.split(" +")));
import static java.util.regex.Pattern.compile;
import java.util.Scanner;
import java.util.regex.Pattern;
Pattern p = compile(" +");
Scanner t = new Scanner(s).useDelimiter(p);
String chunks[] = t.tokens().toArray(String[]::new);
t.close();
import static java.util.regex.Pattern.compile;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
Pattern p = compile(" +");
Scanner t = new Scanner(s).useDelimiter(p);
List<String> chunks = t.tokens().toList();
t.close();
import static java.util.Arrays.asList;
import java.util.List;
List<String> chunks = asList(s.split(" +"));
import static java.util.List.of;
import java.util.List;
List<String> chunks = of(s.split(" +"));
String chunks[] = s.split(" +");
val chunks = s.split("\\s+".toRegex())
 
(defun words (s)
  (if (equalp s "") nil 
      (let ((p (position #\Space s )))
    (cond ((null p) (list s))
          ((zerop p ) (words (subseq s 1)))
          (T (cons (subseq s 0 p) (words (subseq s (+ 1 p ))))))))) 

(setf chunks (words s))
chunks = {}
for substring in s:gmatch("%S+") do
   table.insert(chunks, substring)
end
@import Foundation;
NSArray *chunks=[s componentsSeparatedByString:@" "];
$chunks = preg_split('/ +/', $s);
Uses Classes;
chunks.StrictDelimiter := True;
chunks.Delimiter := ' ';
chunks.DelimitedText := s;
@chunks = split /\s+/, $s;
chunks = s.split()
from re import split
chunks = split(' +', s)
chunks = s.split
let chunks: Vec<_> = s.split_ascii_whitespace().collect();
let chunks: Vec<_> = s.split(' ').collect();
let chunks: Vec<_> = s.split_whitespace().collect();
val chunks = s.split(" ")
(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))))
chunks := s substrings

New implementation...