Filter löschen
Filter löschen

regexprep() skip first occurrence

2 Ansichten (letzte 30 Tage)
newbie9
newbie9 am 13 Mär. 2019
Kommentiert: newbie9 am 13 Mär. 2019
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';

Akzeptierte Antwort

Akira Agata
Akira Agata am 13 Mär. 2019
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 Kommentar
newbie9
newbie9 am 13 Mär. 2019
So simple and clean, thanks so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

newbie9
newbie9 am 13 Mär. 2019
Bearbeitet: newbie9 am 13 Mär. 2019
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)

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by