Reputation: 736
Say the automatic input from the program is:
str = 'John's dog is called Ace'
I want to automatically re-create the string to contain another apostrophe, whenever a single apostrophe is found (as a single apostrophe "breaks" the string):
newstr = 'John''s dog is called Ace'
Note that this has to be done automatically i.e. through some kind of function. I cant just insert a new character manually.
What is the best and most efficient way to do this in Matlab? I am asking question because I know matlab has many functions that ease these tasks and do not necessarily always need whole string traversals (especially useful in very long strings). any kind of help?
Upvotes: 0
Views: 60
Reputation: 1635
One option would be to use strrep
:
strrep('John''s dog''''s called Ace', '''', '''''')
ans =
John''s dog''''s called Ace
It looks odd in the example because of the escaping, so it may not be a good fit if you want it to be readable.
You could replace a '
with char(39), which might cause less problems with errors but more with readability.
I also added a pair of quotes to make sure that worked and, hilariously, this results in a command with 18 single quotes in it. That's a personal best for me anyway.
Upvotes: 3
Reputation: 5672
You could also use regexprep
newStr = regexprep ( str, '''', '''''' )
Upvotes: 3