MATLAB Answers

how will I replace a string with another string without using strrep?

8 ビュー (過去 30 日間)
Hanna Dulay
Hanna Dulay 2019 年 12 月 3 日
コメント済み: Walter Roberson 2019 年 12 月 3 日
function newstring = replace_tags(readstring,tag,replacement);
newstring = "";
i= strfind(r_s,tag);
This is my code so far.

回答 (2 件)

Bhaskar R
Bhaskar R 2019 年 12 月 3 日
Can I use regexprep command ??, If yes
function newstring = replace_tags(readstring,tag,replacement)
% By regular expression
newstring = regexprep(r_s, tag, replacement); % you can do lot of stuff by this

Erivelton Gualter
Erivelton Gualter 2019 年 12 月 3 日
If you still want to keep your line of thought instead of using Bhaskar approach, which is simpler, you can:
% Example entries
readstring = 'This is a good example';
tag = 'good';
replacement = 'great';
% Test
newstring = replace_tags(readstring,tag,replacement)
% Function
function newstring = replace_tags(readstring, tag, replacement)
i = strfind(lower(readstring),tag);
tg = i+length(tag);
newstring = [readstring(1:i-1), replacement, readstring(tg:end)];


Community Treasure Hunt

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

Start Hunting!

Translated by