Replacing String Values By Position

2 ビュー (過去 30 日間)
Joe
Joe 2012 年 9 月 15 日
I wanted to know how I could assign new values to a string based on their position. For example, if I have the following string 'test rest' and I do a strfind('test rest', 'e') and am given the output 2 7. How can I use that to replace the 'e' with another arbitrary value such as 'r', so that my output reads 'trst rrst.' I'd like to do this with out using something like strrep()
Thank you.

採用された回答

Matt Fig
Matt Fig 2012 年 9 月 15 日
You don't need to use linear indexing (STRFIND) here at all. Just use logical indexing.
str = 'test rest';
str(str=='e')='r'

その他の回答 (1 件)

Dishant Arora
Dishant Arora 2012 年 9 月 15 日
str([output])='r'; %or whatevr you want to replace with

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by