Logo

Programming-Idioms

Create the list of integers items for the string s containing integers separated by one or more whitespace characters (space, tab, newline).
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import "fmt"
import "strconv"
parts := strings.Fields(s)
items := make([]int, len(parts))
for i, part := range parts {
	v, err := strconv.Atoi(part)
	if err != nil {
		return err
	}
	items[i] = v
}
import java.util.List;
import java.util.Scanner;
Scanner t = new Scanner(s);
List<Integer> items = t.tokens()
    .map(Integer::parseInt)
    .toList();
t.close();
import java.util.Scanner;
Scanner t = new Scanner(s);
int items[] = t.tokens()
    .mapToInt(Integer::parseInt)
    .toArray();
t.close();
import java.util.Scanner;
Scanner t = new Scanner(s);
Integer items[] = t.tokens()
    .map(Integer::valueOf)
    .toArray(Integer[]::new);
t.close();
import static java.util.stream.Collectors.toCollection;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
Scanner t = new Scanner(s);
List<Integer> items = t.tokens()
    .map(Integer::parseInt)
    .collect(toCollection(ArrayList::new));
t.close();
var
  tmp: array of string;
  items: array of integer;
...  
tmp := s.split(whitespace, TStringsplitOptions.ExcludeEmpty);
SetLength(items, Length(tmp));
for i := Low(tmp) to High(tmp) do items[i] := StrToInt(tmp[i]);
items = [*map(int, s.split())]
items = [int(x) for x in s.split()]
items = s.split.map(&:to_i)