フィルターのクリア

How can I print entered string with restriction of 40 characters per line?

2 ビュー (過去 30 日間)
Sang Heon Lee
Sang Heon Lee 2017 年 9 月 21 日
回答済み: Jan 2017 年 9 月 21 日
So my task is entering a string as a input value. Then, with the numel(string), get the number of characters and divide the entered string in to set of 40 characters per line. Which means whatever character that comes after 40th, 80th, 120th.... it goes down to the subsequent line.
x = input('Please enter your text = ','s');
y = numel(x);
z = y;
i = 0;
while z > 40
z = z - 40; % if y = 83
i = i + 1; % after this, i=2 and z=3
end
for w = 1:40:((40*(i-1))+1)
for k = 40:40:(40*i)
(x(w:k))
end
end
x((40*i) + 1:(40*i) + z)
This is what I currently have and it is not working. For characters which remained (not part of 40 character line) is set as 'z' after the while loop. Then set to print the remaining characters after the for loop. Please help...

採用された回答

Walter Roberson
Walter Roberson 2017 年 9 月 21 日
S = input('Please enter your text = ','s');
parts = regexp(S, '.{1,40}', 'match');
fprintf('%s\n', parts{:});
  1 件のコメント
Stephen23
Stephen23 2017 年 9 月 21 日
編集済み: Stephen23 2017 年 9 月 21 日
+1 briliiant. A very neat use of regular expressions.

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2017 年 9 月 21 日
textwrap(x, 40)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by