regexprep() skip first occurrence

조회 수: 49 (최근 30일)
newbie9
newbie9 2019년 3월 13일
댓글: newbie9 2019년 3월 13일
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일
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일
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)

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by