Matlab 中是否有任何内置函数可以根据字符数切割字符串并将其作为元胞数组或其他内容返回。例如如果调用 A = some_function(string, 3):
Input: string = '1234567890'
Output: A = {'123', '456', '789', '0'}
还是我需要使用循环?
谢谢。
最佳答案
另一种解决方案,稍微更优雅(在我看来),是使用 regexp
:
A = regexp(str, sprintf('\\w{1,%d}', n), 'match')
其中 str
是您的字符串,n
是字符数。
例子
>> regexp('1234567890', '\w{1,3}', 'match')
ans =
'123' '456' '789' '0'
关于string - 按字符数分割字符串matlab,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15691857/