How do I split a string using a only *single* whitespace delimiter so any multiple whitespaces are also "split"?
Show older comments
I am aware of how to use strsplit to split a string using whitespace delimiters. However, if there are multiple whitespaces between two strings of text, the entire whitespace is ignored (as it should be).
Consider the example in which I have a string:
string_line = 'Time Aero Inertial Total'
In this example, each individal string of text in string_line is separated by one whitespace, except between "Inertial" and "Total". In this instance, there are 10 whitespaces.
If I use:
split_string = strsplit(string_line)
I end up with a 1x4 cell, consisting of the strings of text in string_line separated by a whitespace i.e. "Time", "Aero", "Inertial" and "Total".
How do I create a 1x5 cell, in which 8 whitespaces between "Inertial" and "Total" is included in split_string as an empty cell. Thus, split_string should consist of: "Time", "Aero", "Inertial", " " and "Total".
2 Comments
dpb
on 30 May 2022
I presume the general use case will not be fixed-length strings???
I'm certain (well almost certain) a whizard could write a regular expression for the purpose; that someone isn't me, however.
I'd probably take the expedient of introducing delimiters into the string first.
The new(ish) pattern might be able to help build an appropriate expression to pass to split; it's a poor man's way to build regular expressions. I've used it for some very trivial cases, but not something specifically apropos to this. I think you can ask for patterns of at least N characters with it that could possibly let you return the blank field.
Alternatively, how did you get such a string to begin with? Can it be read in such that it maintains the fields one presumes it may have had at one time -- or if it is a header line in a computer-generated output file, then it might be fixed length and/or perthaps one could use an import object to define the format such as to be able to read the five variables; one being interpreted as a missing variable.
deathtime
on 30 May 2022
Accepted Answer
More Answers (2)
I'm totally not the one to ask for elegant regex, but there are always workarounds.
thischar = 'Time Aero Inertial Total';
C = regexp(thischar,'(\s*)','tokenextents');
C = unique([C{:}]);
fdelim = char(10); % pick some delimiter character to insert
thischar(C) = fdelim; % insert delimiters
splitchar = split(thischar,fdelim) % split
I suppose you could always use the indices in C to split the vector directly, but this approach was easy enough.
1 Comment
Jan
on 30 May 2022
For trailing spaces, empty matrices are appended to the output.
The dull method:
s = 'Time Aero Inertial Total';
d = diff(s == ' ');
s(d == -1) = '*'; % Start of non-spaces
s([0, d] == 1) = '*'; % End of non-spaces
s % Just for demonstration:
t = strsplit(s, '*')
With 1 trailing space, a [] is appended to the output. With more trailing spaces, a block of spaces is replied there also.
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!