How to insert a new line before a character in file?
34 views (last 30 days)
Show older comments
Hi, I have a file .txt format:
$text-text-text-text-text-text$text-text-text-text-text-text-text$text-text-text-textc....
I want to convert:
$text-text-text-text-text-text
$text-text-text-text-text-text-text
$text-text-text-text
$text-text-text-text
....
And don't have new line at beginning of text.
Thank you
0 Comments
Answers (2)
Jan
on 26 Dec 2022
Edited: Jan
on 26 Dec 2022
What is the wanted output? A cell string? A string? Another file?
% Perhaps: str = fileread('yourFile.txt');
str = '$text-text-text-text-text-text$text-text-text-text-text-text-text$text-text-text-text';
str = strrep(str, '$', [newline, '$']);
str(1) = [];
And to write this to a file:
writelines(str, 'outFile.txt');
0 Comments
KSSV
on 26 Dec 2022
str = '$text-text-text-text-text-text$text-text-text-text-text-text-text$text-text-text-textc'
s = strsplit(str,'$') ;
s'
3 Comments
Jan
on 26 Dec 2022
You can omit the loop:
for i = 1:length(s)
s{i} = ['$',s{i}] ;
end
using:
s = strcat('$', s)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!