www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/SplitStringAtWhitespaceExample.m
%% Split Strings at Whitespace and Rejoin Them % Split names in a string array at whitespace. Then reorder the strings and % join them so that the last names precede the first names. % % Create a 3-by-1 string array containing names. names = string({'Mary Jones';... 'John Spencer';... 'Elizabeth Young'}) %% % Split |names| at whitespace, making it a 3-by-2 string array. names = split(names) %% % Switch the columns of |names| so that the last names are in the first % column. Add a comma after each last name. names = [names(:,2) names(:,1)]; names(:,1) = names(:,1) + ',' %% % Join the last and first names. The |join| function places a space character % between the strings it joins. After the join, |names| is a 3-by-1 string % array. names = join(names)