How can I include quotes within a string?

237 ビュー (過去 30 日間)
Joshua
Joshua 2013 年 12 月 28 日
コメント済み: Bryan 2020 年 5 月 9 日
I've been trying to make a string: var = '<meta property="og:title" content="(\w*-*\s*\(*\)*&*;*:*\.*,*/*'*)*"/>'; The problem is that the " '* " causes the doublequotes towards the end to become red, and make the string invalid due to the error "Unbalanced or unexpected parenthesis or bracket."

採用された回答

Image Analyst
Image Analyst 2013 年 12 月 28 日
Look up strings in the help and the example shows that you do a double single quote to get a single single quote:
var = '<meta property="og:title" content="(\w*-*\s*\(*\)*&*;*:*\.*,*/*''*)*"/>'

その他の回答 (2 件)

the cyclist
the cyclist 2013 年 12 月 28 日
編集済み: the cyclist 2013 年 12 月 28 日
To you need to use two single quotes to get a single quote inside a string. For example
>> 'You can''t be serious!'
In your case:
var = '<meta property="og:title" content="(\w*-*\s*\(*\)*&*;*:*\.*,*/*''*)*"/>'
should give what you want.
  1 件のコメント
Bryan
Bryan 2020 年 5 月 9 日
That ball was on the line! Chalk flew up!

サインインしてコメントする。


dpb
dpb 2013 年 12 月 28 日
The general rule is that one doubles down on the restricted character...
Example--
>> str='ain''t'
str =
ain't
>>
NB: the doubled quotes to end up with one embedded in the string. One can, of course, also build them by use of the char conversion function--
>> str=['ain' char(39) 't']
str =
ain't
>> q=char(39);
>> str=['ain' q 't']
str =
ain't
>>
The latter shows a temporary variable to shorten the building a little if need more than just one. Sometimes can be easier than trying to work thru the eyestrain sure to result from the double-up exercise; I had more trouble trying to decipher the string you were trying to build than I cared to try to fight thru; hence the example instead.

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by