在 Dart 中,字符串是採用 UTF-16 編碼的序列。github
var s1 = 'Single quotes work well for string literals.';
var s2 = "Double quotes work just as well.";
var s3 = 'It\'s easy to escape the string delimiter.'; var s4 = "It's even easier to use the other delimiter."; var s5 = ''' You can create multi-line strings like this one. ''' 複製代碼
全部的對象都具備 toString()
的函數。express
var intString = 3.1415.toString();
複製代碼
你能夠直接經過 ${expression}
的格式,在字符串中嵌入一個表達式,十分便捷。bash
CoorChice 喜歡這個特性。函數
var s = 'string interpolation';
s = 'That deserves all caps.${s.toUpperCase()} is very handy!'
>>>
That deserves all caps.STRING INTERPOLATION is very handy!
複製代碼
Dart 支持在字符串中直接引用變量,經過 $value
的格式。post
var num = 1000;
var tips = 'The user num is $num.';
print(tips);
>>>
The user num is 1000.
複製代碼
經過 r
前綴,能夠建立強制單行的字符串。ui
// 不使用 r
var s = 'In a raw string, not even \ngets special treatment.';
>>>
In a raw string, not even
gets special treatment.
// 使用 r
var s = r'In a raw string, not even \ngets special treatment.';
>>>
In a raw string, not even \ngets special treatment.
複製代碼
在 Dart 中,比較兩個字符串能夠使用 ==
運算符:this
String s1 = 'This is String';
var s2 = 'This is String';
print(s1 == s2);
>>>
true複製代碼