0%

[Flutter讀書會]Dart101:Built-inTypes資料型別-Strings

前提情要

上篇介紹完資料型別-Numbers後,這篇來到Strings的篇幅,筆者這邊也是採用官方文件及相關連結做一個簡述及驗證,跟著筆者一起學習吧。

內容

筆者這邊還是簡單列一下筆者這邊的開發環境

  • 作業系統:Mac OS
  • Dart版本:2.17.6
  • 編輯器:Visual Studio Code

Strings

宣告方式

Dart語言中,Strings代表UTF-16編碼的序列組成,可以使用單引號雙引號來表示字串

1
2
3
4
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.";

動態組合字串

透過${*expression*}可以動態組合字串

1
2
3
4
5
6
7
8
9
var s = 'string interpolation';

assert('Dart has $s, which is very handy.' ==
'Dart has string interpolation, '
'which is very handy.');
assert('That deserves all caps. '
'${s.toUpperCase()} is very handy!' ==
'That deserves all caps. '
'STRING INTERPOLATION is very handy!');

==比對字串,要注意的是Dart編譯器比對的是已經轉換成UTF-16編碼過後的一組序列的比對

+運算子

可以透過+運算子組合字串

1
2
var s2 = 'The + operator ' + 'works, as well.';
assert(s2 == 'The + operator works, as well.');

換行宣告字串

若宣告字串中,需要換行宣告,又要保持同一行的字串時,可透過'''三個單引號來表示

1
2
3
4
5
6
7
var s1 = '''
You can create
multi-line strings like this one.
''';

var s2 = """This is also a
multi-line string.""";

Strings常用操作方法

  1. 搜尋字串
    1. contains
    2. startsWith
    3. endsWith
    4. indexOf
1
2
3
4
5
6
7
8
9
10
11
// Check whether a string contains another string.
assert('Never odd or even'.contains('odd'));

// Does a string start with another string?
assert('Never odd or even'.startsWith('Never'));

// Does a string end with another string?
assert('Never odd or even'.endsWith('even'));

// Find the location of a string inside a string.
assert('Never odd or even'.indexOf('odd') == 6);
  1. 擷取字串SubString
    1. substrings
    2. split
    3. codeUnits
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Grab a substring.
assert('Never odd or even'.substring(6, 9) == 'odd');

// Split a string using a string pattern.
var parts = 'progressive web apps'.split(' ');
assert(parts.length == 3);
assert(parts[0] == 'progressive');

// Get a UTF-16 code unit (as a string) by index.
assert('Never odd or even'[0] == 'N');

// Use split() with an empty string parameter to get
// a list of all characters (as Strings); good for
// iterating.
for (final char in 'hello'.split('')) {
print(char);
}

// Get all the UTF-16 code units in the string.
var codeUnitList = 'Never odd or even'.codeUnits.toList();
assert(codeUnitList[0] == 78);
  1. 轉大、小寫UpperCase/LowerCase
    1. toUpperCase
    2. toLowerCase
1
2
3
4
5
// Convert to uppercase.
assert('web apps'.toUpperCase() == 'WEB APPS');

// Convert to lowercase.
assert('WEB APPS'.toLowerCase() == 'web apps');
  1. TrimAndEmpty
    1. trim
    2. isEmpty
    3. isNotEmpty
1
2
3
4
5
6
7
8
// Trim a string.
assert(' hello '.trim() == 'hello');

// Check whether a string is empty.
assert(''.isEmpty);

// Strings with only white space are not empty.
assert(' '.isNotEmpty);
  1. 取代Replace
1
2
3
4
5
var greetingTemplate = 'Hello, NAME!';
var greeting = greetingTemplate.replaceAll(RegExp('NAME'), 'Bob');

// greetingTemplate didn't change.
assert(greeting != greetingTemplate);

Strings相關詳細API部份,請參考

https://api.dart.dev/stable/2.17.6/dart-core/String-class.html

結論

筆者這邊有跳過一個主題:Runes and grapheme clusters,這個需要花滿多時間去研讀並消化它,畢竟很底層的操作才會需要使用到它,改天再找時間完成另一篇專門文章介紹該主題,剛好筆者參加的另一個長青讀書會(因為已經維持五六年有了)的成員上週在報Encoding相關的主題,筆者屆時會參考該成員提供的教學影片,就先放在參考中提醒筆者,下篇見。

參考