How to add a newline after a certain number of characters

31 views (last 30 days)
function [strMultiLines] = nCharLine(strtxt, n)
Length=length(strtxt);
strMultiLines=strtxt;
strMultiLines=[strMultiLines(1:n) char(10) strMultiLines(n+1:length(strMultiLines))];
end
This is my code, and It gives me this kind of output when run
'Hi th
ere, Im Jack'
What I want, is for the code to keep splitting after a certain number of characters. How can I do this? I have tried using a while loop, yet it didn't work for me.

Answers (2)

dpb
dpb on 1 Dec 2020
function strMultiLines=nCharLine(strtxt, n)
fmt=[repmat('%c',1,n) '\n'];
strMultiLines=sprintf(fmt,strtxt);
end
for input char() array. If cellstr, must dereference it for sprintf

Walter Roberson
Walter Roberson on 1 Dec 2020
while number of remaining characters > n
remove and record the first n characters
add them and newline to output
end
add remaining characters to output
However suppose that you set n to (say) 4, and there is an exact multiple of 4 characters in the input:
'ABCDEFGH'
then clearly you should split after the first group of 4,
'ABCD|EFGH'
but should you also split at the end of the group?
'ABCD|EFGH|'
? Doing so would follow the rule about adding a newline after a certain number of characters... but is that really the rule you want? Is perhaps the real rule about splitting splitting the string every N characters? Because "splitting" implies that you do not put on a trailing newline if it would be at the end.
  11 Comments
John Jamieson
John Jamieson on 5 Dec 2020
Never mind I think I got it, thank you for your help. Your advice really helped me
Walter Roberson
Walter Roberson on 5 Dec 2020
Suppose n were 7, then the desired output would be
Hi
there,
Im Jack
and if n were 9 then the desired output would be
Hi there,
Im Jack
and for 12 it would be
Hi there, Im
Jack
That is goes roughly:
if there are less than n characters left,
emit the remaining characters
else
look forward n characters
starting there,
if the current character is space
emit the buffer up to but excluding the current character
emit end of line
remove from the beginning of the buffer up to the current character
stop checking here
else
move backwards and check again
end
end
This does not tell you what to do if you are positioned to a word longer than n. Historically, there are several different solutions that have been used:
  • break the line right where you are, after the n'th character
  • keep looking forward and break at the first whitespace
  • insert a dash before the current character and put in a break -- supe-\ncali-\nfrag-\nilis-\n
  • analyze the current word to determine where its syllables are, and break at the syllable boundary before the current character
  • analyze the current word to determine where its syllables are, and break at the preferred syllable boundary if it is within a certain number of characters before the current character, and otherwise break at the syllable boundary closest to the current character
The versions that examine syllables can be a lot more work to implement. Especially in English, as there are words whose syllable breaks depend upon context, like unionized which can be un-ion-ized (pertaining to labour unions) or un-i-on-ized (pertaining to chemical ions)

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!