創建字元串
要創建一個字元串,我們可以使用單引號或雙引號或三引號:
void main() {
String a = 'abc'; //單引號
String b = "abc"; //雙引號
//三引號
String c = '''
abc
def
''';
}
字元串拼接
多個字元串可使用 + 號進行拼接
void main() {
String a = 'abc';
String b = "abc";
print(a + b);
}
獲取單個字元
可使用下標的方式獲取單個字元
void main() {
String a = 'abc';
print(a[1]);
}
常用的屬性
屬性 | 類型 | 描述 |
codeUnits | List<int> | 獲取字元串utf-16編碼值的列表 |
hashCode | int | 獲取字元派生的哈希代碼 |
isEmpty | bool | 字元串是否為空 |
isNotEmpty | bool | 字元串是否不為空 |
length | int | 獲取字元串長度 |
runes | Runes | 獲取字元串utf-16編碼值的可迭代列表 |
runtimeType | Type | 獲取運行時的數據類型 |
void main() {
String a = 'abcdefg';
print(a.codeUnits);
print(a.hashCode);
print(a.isEmpty);
print(a.isNotEmpty);
print(a.length);
print(a.runes);
print(a.runtimeType);
//執行結果
//[97, 98, 99, 100, 101, 102, 103]
//180814200
//false
//true
//7
//(97, 98, 99, 100, 101, 102, 103)
//String
}
常用的屬性
方法 | 類型 | 描述 |
codeUnitAt(int index) | int | 返回給定索引值的對應utf-16編碼 |
compareTo(String other) | int | 與傳入字元串進行比較,有相同返回1,否則返回-1 |
contains(Pattern other, [int index]) | bool | 查找返回字元串是否有符號要求的,傳入index規定從index位開始查找 |
endsWith(String other) | bool | 字元串的結尾是否為傳入的值 |
indexOf(Pattern other, [int start]) | int | 從字元串前面開始查找返回符合規則的第一個的索引位置,傳入start規定從哪裡開始查找 |
lastIndexOf(Pattern other, [int start]) | int | 與indexOf相同,不同的是這個方法是從後面開始查找 |
padLeft(int width, [String padding]) | String | 如果字元串沒有width的長度,則在前面加上padding字元串並返回,不會改變原字元串 |
padRight(int width, [String padding]) | String | 同padLeft方法相同,不同的是從padding加在後面 |
replaceAll(Pattern from, String to) | String | 替換所有匹配的子字元串 |
replaceAllMapped(Pattern from, String Function(Match match) replace) | String | 將匹配到的字元串用函數處理後返回字元串替換 |
replaceFirst(Pattern from, String to, [int index]) | String | 替換第一個匹配的子字元串,可以規定從index出開始匹配 |
replaceFirstMapped(Pattern from, String replace(match), [int index]) | String | 同replaceAllMapped,此方法只替換第一個匹配的值,可以規定從index處開始匹配 |
replaceRange(int start, int end, String to) | String | 替換範圍內的字元串 |
split(Pattern pattern) | List<String> | 拆分字元串為數組 |
splitMapJoin(Pattern pattern, { String onMatch(match), String onNonMatch(match) }) | String | 拆分替換字元串,匹配和不匹配的執行對應函數替換成返回值 |
startsWith(Pattern pattern, [int index]) | bool | 是否是匹配的正則或字元串開始,可以規定從index開始查找 |
substring(int startIndex, [int endIndex]) | String | 提取字元串中startIndex(包含)到endIndex(不包含)兩個指定的索引號之間的字元。 |
toLowerCase() | String | 把字元串轉換為小寫 |
toUpperCase() | String | 把字元串轉換為大寫 |
trim() | String | 去除字元串兩邊的空白 |
trimLeft() | String | 去除字元串左邊的空白 |
trimRight() | String | 去除字元串右邊的空白 |
void main() {
String a = ' abacdfefg ';
print(a.codeUnitAt(0));
print(a.compareTo('other'));
print(a.contains('b'));
print(a.endsWith('f'));
print(a.indexOf('a'));
print(a.lastIndexOf('f'));
print(a.padLeft(20, '0'));
print(a.padRight(20, '0'));
print(a.replaceAll(r'a', 'h'));
print(a.replaceAllMapped(r'a', (d) => d.hashCode.toString()));
print(a.replaceFirst(r'a', 'h'));
print(a.replaceFirstMapped(r'a', (d) => d.hashCode.toString()));
print(a.replaceRange(0, 5, 'h'));
print(a.split(r'f'));
print(a.splitMapJoin(r'f'));
print(a.startsWith(r'f', 2));
print(a.substring(2, 6));
print(a.toLowerCase());
print(a.toUpperCase());
print(a.trim());
print(a.trimLeft());
print(a.trimRight());
//輸出結果
//32
//-1
//true
//false
//2
//9
//0000000 abacdfefg
// abacdfefg 0000000
// hbhcdfefg
// 336578979b372031767cdfefg
// hbacdfefg
// 994399439bacdfefg
//hcdfefg
//[ abacd, e, g ]
// abacdfefg
//false
//abac
// abacdfefg
// ABACDFEFG
//abacdfefg
//abacdfefg
// abacdfefg
}
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/268232.html