4.我爲何喜歡用Dart中的字符串?

目錄傳送門:《Flutter快速上手指南》先導篇
git

在 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()

全部的對象都具備 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複製代碼

目錄傳送門:《Flutter快速上手指南》先導篇
編碼

如何找到我?

傳送門:CoorChice 的主頁spa

傳送門:CoorChice 的 Github

相關文章
相關標籤/搜索