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"
댓글 수: 0
채택된 답변
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
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)};
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!