如何在字符串中加上单引号? [英] How can I put single quotes in a string?
问题描述
我在MATLAB中遇到字符串问题,默认字符串为C:\Users\Root\Downloads\Path
.我想像这样的'C:\Users\Root\Downloads\Path\'
那样用单引号使它成为字符串.与其他编程语言一样,我多次尝试使用反斜杠对字符串进行转义,但MATLAB却没有这样做,我不知道如何解决此问题.
I'm facing a problem with string in MATLAB the default string is C:\Users\Root\Downloads\Path
. I want to make this string with single quotes inside it like this 'C:\Users\Root\Downloads\Path\'
. I try many times to escape the string with backslash like other programming languages but MATLAB didn't doing this i don't know how to fix this problem.
代码:
clear all
clc
s='C:\Users\Root\Downloads\Path';
str=fprintf('%s',s);
推荐答案
技巧是使用两个引号而不是一个引号:
The trick is to use two quotes instead of one:
s='''C:\Users\Root\Downloads\Path''';
str=fprintf('%s',s)
'C:\Users\Root\Downloads\Path'
str =
30
请注意,str
将是数字30
,因为fprintf
返回其打印的字符数,而不是字符串本身!如果只需要字符串,那么第一行就足够了.
Note that str
will be the number 30
, since fprintf
returns the number of characters it prints, not the string itself! If you just want the string, then the first line is enough.
disp(s)
'C:\Users\Root\Downloads\Path'
请注意,MATLAB中没有数据类型"String".您有一个字符数组.
Note that there is no data type "String" in MATLAB. You have an array of characters.
这篇关于如何在字符串中加上单引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!