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

조회 수: 3 (최근 30일)
Hanna Dulay
Hanna Dulay 2019년 12월 3일
댓글: Walter Roberson 2019년 12월 3일
function newstring = replace_tags(readstring,tag,replacement);
newstring = "";
r_s=lower(readstring);
i= strfind(r_s,tag);
tg=i+length(tag);
final=readstring(i:tg)
end
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)
r_s=lower(readstring);
% By regular expression
newstring = regexprep(r_s, tag, replacement); % you can do lot of stuff by this
end

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)];
end

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by