Change all indices of a word in a string to '*'.
1 回表示 (過去 30 日間)
古いコメントを表示
What is the best method to change all indices of a certain word in a string to '*'.
Like if my string were 'Hello, my name is bob, my age is 43.' And I want to change bob and age so it outputs 'Hello, my name is ***, my *** is 43.'
0 件のコメント
回答 (1 件)
Guillaume
2015 年 3 月 3 日
編集済み: Guillaume
2015 年 3 月 3 日
1 件のコメント
Joseph Cheng
2015 年 3 月 3 日
to expand on Guillaume's answer, since names are probably not going to stay 3 char long.
you can get the position and items using regexp:
filters = {'bob'; 'age'}';
teststr = 'Hello, my name is bob, my age is 43.';
regpattern = sprintf(strjoin(filters, '|'));
matches = regexp(teststr, regpattern, 'match')
pos= regexp(teststr, regpattern)
then knowing what was matched and where the positions are you can replace those index positions with "*".
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!