string - Replace substring that lies between two positions -


i have string s in matlab. how can replace substring in s pattern p. know first , last index of substring in s. approach?

how that?

str = 'my dog called jim';      %// original string    = 4;                             %// starting index b = 6;                             %// last index  replace = 'hamster';               %// new pattern  newstr = [str(1:a-1) replace str(b+1:end)] 

returns:

newstr =  hamster called jim 

in case pattern want substitute has same number of characters new one, can use simple indexing:

str(a:b) = 'cat' 

returns:

str =  cat called jim 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -