Flutter / Dart中单引号和双引号之间的区别 [英] Difference between single and double quotes in Flutter/Dart
问题描述
我知道单引号和双引号在Dart中至少具有某种程度的对等性。例如,
I know that single and double quotes have at least some level of equivelence in Dart. For example,
var myString = "Hello world"; // double quotes
和
var myString = 'Hello world'; // single quotes
据我所知没有程序上的区别。
have no programmatic difference to my knowledge.
我一直看到它们在各种示例和某些文档中似乎可以互换使用。我想知道我是否缺少细微的差别,或者是否有推荐的风格可以遵循,尤其是在Flutter中。
I keep seeing them used seemingly interchangeably in various examples and in some documentation. I'm wondering if there is a subtle difference that I am missing or if there is a recommended style to follow, especially in Flutter.
这是一个问答集。 ;阅读Flutter和Dart样式指南后的自我解答。
推荐答案
单引号和双引号都适用于Dart
Single and double quotes both work in Dart
final myString = 'hello';
与
final myString = "hello";
需要转义分隔符
使用 \
反斜杠可在单引号字符串中转义单引号。
Delimiters need to be escaped
Use a \
backslash to escape single quotes in a single quote string.
final myString = 'Bob\'s dog'; // Bob's dog
用双引号将双引号转义也可以。
Same thing to escape double quotes in a double quote string.
final myString = "a \"quoted\" word"; // a "quoted" word
但如果分隔符不同,则无需转义任何内容。
But no need to escape anything if the delimiter is different.
final myString = "Bob's dog"; // Bob's dog
final myString = 'a "quoted" word'; // a "quoted" word
也无需担心传递给插值字符串的值。
Also no need to worry about the value passed into an interpolated string.
final value = '"quoted"'; // "quoted"
final myString = "a $value word"; // a "quoted" word
在Flutter中首选单引号
Flutter样式指南建议对所有内容使用单引号
Prefer single quotes in Flutter
The Flutter style guide recommends using single quotes for everything
final myString = 'hello';
除了嵌套字符串
print('Hello ${name.split(" ")[0]}');
或包含单引号的字符串(可选)
or strings containing single quotes (optional)
final myString = "Bob's dog";
final myString = 'Bob\'s dog'; // ok
Dart样式指南在此问题上似乎保持沉默。
The Dart style guide appears to be silent on the issue.
这篇关于Flutter / Dart中单引号和双引号之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!