How can I replace a certain pattern in a string?
7 次查看（过去 30 天）
Hi! I'm trying to replace a certain pattern in a string using strrep. The original string is like 'abbabba', and I want to replace all 'abba' into 'aaaa'. The expected result is 'aaaaaaa' (7 'a's). I tried the following command:
Note that the result has 8 'a's, while 7 'a's are expected. I wonder how can I get the desired result. Thanks so much!
oldstr = 'abbabba';
oldp = 'abba';
newp = 'aaaa';
newstr = regexprep(regexprep(oldstr,oldp,newp),oldp,newp);
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!