Split a string on ' ' and 'ain', treating multiple delimiters as one. Specify multiple delimiters in a cell array of strings.
str = 'The rain in Spain stays mainly in the plain.';
[C,matches] = strsplit(str,{' ','ain'},'CollapseDelimiters',true)
C =
'The' 'r' 'in' 'Sp' 'stays' 'm' 'ly' 'in' 'the' 'pl' '.'
matches =
' ' 'ain ' ' ' 'ain ' ' ' 'ain' ' ' ' ' ' ' 'ain'
Split the same string on whitespace and on 'ain', using regular expressions and treating multiple delimiters separately.
[C,matches] = strsplit(str,{'\s','ain'},'CollapseDelimiters',...
false, 'DelimiterType','RegularExpression')
C =
'The' 'r' '' 'in' 'Sp' '' 'stays' 'm' 'ly' 'in' 'the' 'pl' '.'
matches =
' ' 'ain' ' ' ' ' 'ain' ' ' ' ' 'ain' ' ' ' ' ' ' 'ain'
In this case, strsplit treats the two delimiters separately, so empty strings appear in output Cbetween the consecutively matched delimiters.
本文介绍如何使用MATLAB的strsplit函数来高效地处理字符串拆分任务。通过指定多个分隔符并选择不同的处理方式(如合并连续的分隔符或将空字符串视为单独的输出),可以实现灵活的数据预处理。此外,还展示了如何利用正则表达式来增加拆分的灵活性。
445

被折叠的 条评论
为什么被折叠?



