Replace letter with dot

5 ビュー (過去 30 日間)
Dos Zhu
Dos Zhu 2020 年 5 月 4 日
コメント済み: Star Strider 2020 年 5 月 4 日
Does anybody know how to replace first letter as well as same subsequent letters of a word with dot in a string?
For instance:
'MOMENT AND INITIALIZATION'
i need: '.O.ENT AND .N.T.AL.ZAT.ON'
To note, I need all white spaces to be saved as they are.

回答 (1 件)

Star Strider
Star Strider 2020 年 5 月 4 日
Try this:
str = 'MOMENT AND INITIALIZATION';
newstr = str;
old = {'M','I'}';
for k = 1:numel(old)
newstr = strrep(newstr, old(k),{'.'});
end
newstr
producing:
newstr =
1×1 cell array
{'.O.ENT AND .N.T.AL.ZAT.ON'}
  2 件のコメント
Dos Zhu
Dos Zhu 2020 年 5 月 4 日
what if I prompt user to enter words?
Star Strider
Star Strider 2020 年 5 月 4 日
First, I would use the inputdlg function for tthat.
Second, the strrep function is case-sensitive, so you would need to account for that. It would likely be easiest to use the upper function to convert the input string to all uppercase, then do the replacements.

サインインしてコメントする。

カテゴリ

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