hi, how to use strsplit function to split a line of string into multiple lines when a predefined number of characters is reached?

5 次查看(过去 30 天)
for example: if I have: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", I want to split it into lines after 4 characters and then I could have:"AAAA" "AAAA" "AAAA" "AAAA"

采纳的回答

Star Strider
Star Strider 2015-9-17
This divides them into 8 rather than 4 strings:
str = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
SplitLen = 4;
CheckLen = SplitLen*fix(length(str)/SplitLen); % So ‘reshape’ Will Work
Result = {reshape(str(1:CheckLen), [], 4)}; % Result As Cell Array
  6 个评论
sahar hemmati
sahar hemmati 2015-9-17
still it does not work, because if the length of remainder of string is less than what was defined it will be ignored and i don't want this
Star Strider
Star Strider 2015-9-17
The string argument to reshape has to be an integer multiple of the desired length of the substrings. The easiest way to deal with the remainder of the string is to append it to the end:
Result = {reshape(str(1:CheckLen), [], SplitLen); str(CheckLen+1:end)};

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2015-9-17
s=reshape(s,L,[]);
Requires rem(length(S)/L)=0, of course.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by