How to insert a new line before a character in file?
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
Answers (2)
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');
str = '$text-text-text-text-text-text$text-text-text-text-text-text-text$text-text-text-textc'
s = strsplit(str,'$') ;
s'
3 Comments
Khánh Tân
on 26 Dec 2022
str = '$text-text-text-text-text-text$text-text-text-text-text-text-text$text-text-text-text' ;
s = strsplit(str,'$')' ;
s(1) = [] ;
for i = 1:length(s)
s{i} = ['$',s{i}] ;
end
s
Jan
on 26 Dec 2022
You can omit the loop:
for i = 1:length(s)
s{i} = ['$',s{i}] ;
end
using:
s = strcat('$', s)
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!