Here is a regex-based formula similar to Rubén's answer:
=arrayformula({regexextract(A1:A3,"^[^ ]+"), regexextract(A1:A3,"[^ ]+$")})
Here, ^[^ ]+
means: all non-space characters at the beginning of string, and [^ ]+$
means: all non-space characters at the end of string. One can replace space by comma, etc: for example, [^,]
This is only meant for strings containing exactly one separator: otherwise, the output will be wrong.
Custom function
To handle splitting into an arbitrary number of strings, I wrote a custom function which can be used as arraysplit(A1:A3, " ")
. It accepts column ranges (or single cells) as the first parameter, and separator as the second parameter.
Unlike the built-in split
function, this one does not ignore empty strings: e.g., a,b,,c
split by comma becomes four cells, the third of them blank. (In my opinion, ignoring empty strings is a major flaw in the design of the built-in split
function.)
/**
* Splits a column of strings by a separator.
*
* @param {A1:A3} range Column range or a single cell
* @param {" "} separator Separating character or substring
* @returns Substrings, including empty strings
* @customfunction
*/
function arraysplit(range, separator) {
if (range.constructor !== Array) {
range = [[range]]; // handle single-cell input
}
if (range[0].length !== 1) {
throw new Error('First argument must be a column range');
}
var i, j, split, output = [], width = 1;
for (i = 0; i < range.length; i++) {
split = range[i][0].split(separator);
output.push(split);
width = Math.max(width, split.length); // max number of pieces
}
for (i = 0; i < output.length; i++) {
for (j = 0; j < width - output[i].length; j++) {
output[i].push(''); // empty string to make rectangular array
}
}
return output;
}
Bonus feature: unlike the built-in split, arraysplit
accepts empty string as a separator, in which case it splits the strings into separate characters. (Of course, this behavior is simply inherited from the JavaScript's split method.)
=arrayformula(split(A1:A3," "))
but it doesn't work for me as desired anyway.