Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.


Daniel
Posts:
12
Registered:
9/29/12


What is the best way to replace certain characters in a string?
Posted:
Sep 29, 2012 10:07 PM


Hello everyone,
I have a cell array of strings (each cell contains one string). What I need to do is to search for a certain key word in each cell and replace it with another.
For example,
a{1}='I like dogs.'; a{2}='I have three dogs.'; a{3}='One of the dogs is yellow.';
Now say I want to replace all the "dog" with "cat". In each string, the word "dog" occurs at most once. So I do the following
for m=1:length(a) k=strfind(a{m},'dog'); if ~isempty(k) a{m}=[a{m}(1:k1) 'cat' a{m}(k+1:end)]; end end
It works but pretty slow for large cell array. Any suggestions on the implementation? I appreciate it.
Dan



