Search code examples
stringmatlabapostrophe

Doubling the number of apostrophes in a string Matlab


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?


Solution

  • You could also use regexprep

    newStr = regexprep ( str, '''', '''''' )