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
Post a Comment