regexprep() skip first occurrence

Is there a way to use regexprep() but skip the first occurrence of a space?
mystring = 'this is my string';
desired return:
mystring2 = 'this ismystring';

 採用された回答

Akira Agata
Akira Agata 2019 年 3 月 13 日

0 投票

How about using regexp to find the position of spaces, and delete 2nd~Nth spaces? Like:
mystring = 'this is my string';
pos = regexp(mystring,'\s');
mystring(pos(2:end)) = [];

1 件のコメント

newbie9
newbie9 2019 年 3 月 13 日
So simple and clean, thanks so much!

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

その他の回答 (1 件)

newbie9
newbie9 2019 年 3 月 13 日
編集済み: newbie9 2019 年 3 月 13 日

0 投票

this works but perhaps is not most efficient:
[spaces,letters] = regexp(mystring, ' ', 'match', 'split', 'forceCellOutput');
spaces = [spaces{:}];
letters = [letters{:}];
mystring2 = [sprintf('%s',letters{2:end-1}), letters{end}];
mystring2 = strcat(char(letters(1)), {' '}, mystring2)

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

タグ

質問済み:

2019 年 3 月 13 日

コメント済み:

2019 年 3 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by