Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: What is the best way to replace certain characters in a string?
Replies: 4   Last Post: Sep 30, 2012 9:50 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Daniel

Posts: 10
Registered: 9/29/12
What is the best way to replace certain characters in a string?
Posted: Sep 29, 2012 10:07 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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:k-1) '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



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.