Dart语法基础系列五《String 源码API详解》,2024年最新计算机大学生毕业设计题目

int get length;

例子:

‘Dart’.length; // 4

‘Dart’.runes.length; // 4

var clef = ‘\u{1D11E}’;

clef.length; // 2

clef.runes.length; // 1

== 等号操作符

检查每个code units 是否相等,不比较Unicode的等价性。

API源码:

bool operator ==(Object other);

compareTo

作用: 比较字符串是否相,返回整型,0相等,-1不相等。

API源码:

int compareTo(String other);

例子:

var a = ‘a’;

var b = ‘a’;

var c = ‘c’;

print(a.compareTo(b)); //0

print(a.compareTo©); //-1

endsWith

作用: 判断是否以XX字符串结尾

API源码:

bool endsWith(String other);

例子:

‘Dart’.endsWith(‘t’); // true

startsWith

作用: 判断字符串是否已XX开头

API源码:

bool startsWith(Pattern pattern, [int index = 0]);

参数一: Pattern 是String,RegExp 都去实现的抽象类。

参数二: 右偏移个数

例子:

var start = ‘我爱你中国’;

print(start.startsWith(‘你’, 2)); //true

也可以跟正则:

print(‘dart’.startsWith(RegExp(r’art’), 1)); //true

indexOf

作用: 匹配查找字符串的位置,返回int 位置

API源码:

/// Returns the position of the first match of [pattern] in this string,

/// starting at [start], inclusive:

/// var string = ‘Dartisans’;

/// string.indexOf(‘art’); // 1

/// string.indexOf(RegExp(r’[A-Z][a-z]')); // 0

/// Returns -1 if no match is found:

/// string.indexOf(RegExp(r’dart’)); // -1

/// The [start] must be non-negative and not greater than [length].

int indexOf(Pattern pattern, [int start = 0]);

搜索字符在字符串的位置, 参数类似 startsWith。

例子:

print(‘dart’.indexOf(‘art’)); //1

print(‘dart’.indexOf(‘t’)); //3

print(‘dart’.indexOf(‘b’)); //-1

print(‘dart’.indexOf(RegExp(r’dart’))); //0

print(‘dart’.indexOf(‘d’)); //0

print(‘dart’.indexOf(‘d’, 1)); //-1

print(‘aabbbcc’.indexOf(‘b’)); //2

print(‘aabbbcc’.indexOf(‘b’, 3)); //3

lastIndexOf

作用: 匹配查找字符串的最后一个的位置,返回int 位置

API源码:

/// The starting position of the last match [pattern] in this string.

///

/// Finds a match of pattern by searching backward starting at [start]:

/// var string = ‘Dartisans’;

/// string.lastIndexOf(‘a’); // 6

/// string.lastIndexOf(RegExp(r’a(r|n)')); // 6

/// Returns -1 if [pattern] could not be found in this string.

/// string.lastIndexOf(RegExp(r’DART’)); // -1

/// If [start] is omitted, search starts from the end of the string.

/// If supplied, [start] must be non-negative and not greater than [length].

int lastIndexOf(Pattern pattern, [int? start]);

同理 indexOf 只是会选择最后一个匹配的位置

isEmpty 和 isNotEmpty

作用: 判断字符串是否是空

API源码:

/// Whether this string is empty.

bool get isEmpty;

/// Whether this string is not empty.

bool get isNotEmpty;

因为空安全所以不存在null去判断这种情况。

例子:

String a = ‘’;

print(a.isEmpty);

print(a.isNotEmpty);

substring

作用: 字符串截取子字符串

API源码:

/// The substring of this string from [start],inclusive, to [end], exclusive.

///

/// Example:

/// var string = ‘dartlang’;

/// string.substring(1); // ‘artlang’

/// string.substring(1, 4); // ‘art’

String substring(int start, [int? end]);

例子:

var str = ‘我爱你中国’;

var subStr1 = str.substring(1);

print(subStr1);

var subStr2 = str.substring(1, 3);

print(subStr2);

打印结果:

爱你中国

爱你

trim

作用: 去除字符串空白格

‘\tDart is fun\n’.trim(); // ‘Dart is fun’

例子:

去除空白格返回的字符串还是之前的地址:

var str1 = ‘Dart’;

var str2 = str1.trim();

identical(str1, str2); // true

去除空白格的uniCode 值如下:

/// 0009…000D ; White_Space # Cc …

/// 0020 ; White_Space # Zs SPACE

/// 0085 ; White_Space # Cc

/// 00A0 ; White_Space # Zs NO-BREAK SPACE

/// 1680 ; White_Space # Zs OGHAM SPACE MARK

/// 2000…200A ; White_Space # Zs EN QUAD…HAIR SPACE

/// 2028 ; White_Space # Zl LINE SEPARATOR

/// 2029 ; White_Space # Zp PARAGRAPH SEPARATOR

/// 202F ; White_Space # Zs NARROW NO-BREAK SPACE

/// 205F ; White_Space # Zs MEDIUM MATHEMATICAL SPACE

/// 3000 ; White_Space # Zs IDEOGRAPHIC SPACE

///

/// FEFF ; BOM ZERO WIDTH NO_BREAK SPACE

trimLefttrimRight分别去除左边和右边。

乘号

作用: 就是字符串重复repeat

字符串 = 字符串 * 个数

API源码:

String operator *(int times);

例子:

var times = ‘哈’;

var timesRepeat = times * 10;

print(timesRepeat); //哈哈哈哈哈哈哈哈哈哈

padLeft 和 padRight

作用: 字符串补齐

API源码:

String padLeft(int width, [String padding = ’ ']);

String padRight(int width, [String padding = ’ ']);

例子:

多用于字符串补0处理,举例padleft,padRight同理。

var list = [];

for (var i = 0; i < 100; i++) {

list.add(i.toString().padLeft(4, ‘0’));

}

print(list); //[0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007, 0008, 0009, 0010, 0011, 0012, 0013, 0014, 0015,…]

contains

作用: 判断字符串是否包含子字符串或者正则

APi源码:

/// Example:

/// var string = ‘Dart strings’;

/// string.contains(‘D’); // true

/// string.contains(RegExp(r’[A-Z]')); // true

/// If [startIndex] is provided, this method matches only at or after that

/// index:

/// string.contains(‘D’, 1); // false

/// string.contains(RegExp(r’[A-Z]'), 1); // false

/// The [startIndex] must not be negative or greater than [length].

bool contains(Pattern other, [int startIndex = 0]);

例子:

print(‘Dart’.contains(‘D’)); // true

print(‘Dart’.contains(‘D’, 1)); // false

replaceFirst

作用: 替换掉第一个符合条件的字符

参数:

  • from 被替换字符支持正则查找和字符串 ;

  • to是要替换的字符;

  • startIndex开始位置

API源码:

String replaceFirst(Pattern from, String to, [int startIndex = 0]);

例子:

var replaceFristResult = ‘DartD’.replaceFirst(‘D’, ‘X’);

print(replaceFristResult);

var replaceFristResult1 = ‘DartD’.replaceFirst(‘D’, ‘X’, 1);

print(replaceFristResult1);

var replaceFristResult2 = ‘DartD’.replaceFirst(r’D’, ‘X’, 1);

print(replaceFristResult2);

replaceFirstMapped

作用: 相比replaceFirst,替换字符可以根据匹配match结果去替换,更加灵活。

API源码:

/// Replace the first occurrence of [from] in this string.

///

/// Returns a new string, which is this string

/// except that the first match of [from], starting from [startIndex],

/// is replaced by the result of calling [replace] with the match object.

///

/// The [startIndex] must be non-negative and no greater than [length].

String replaceFirstMapped(Pattern from, String replace(Match match),

[int startIndex = 0]);

例子:

String replace(Match match) {

print(match.start);

print(match.end);

print(match.groupCount);

print(match.input);

print(match.pattern);

return ‘X’;

}

var replaceFristResult = ‘DartD’.replaceFirstMapped(‘D’, replace, 1);

print(replaceFristResult);

replaceAll和 replaceAllMapped

作用: 类似replaceFirst ,replaceAllMapped只是全部进行替换。

API源码:

/// Replaces all substrings that match [from] with [replace].

///

/// Creates a new string in which the non-overlapping substrings matching

/// [from] (the ones iterated by from.allMatches(thisString)) are replaced

/// by the literal string [replace].

/// ‘resume’.replaceAll(RegExp(r’e’), ‘é’); // ‘résumé’

/// Notice that the [replace] string is not interpreted. If the replacement

/// depends on the match (for example on a [RegExp]'s capture groups), use

/// the [replaceAllMapped] method instead.

String replaceAll(Pattern from, String replace);

/// Replace all substrings that match [from] by a computed string.

///

/// Creates a new string in which the non-overlapping substrings that match

/// [from] (the ones iterated by from.allMatches(thisString)) are replaced

/// by the result of calling [replace] on the corresponding [Match] object.

///

/// This can be used to replace matches with new content that depends on the

/// match, unlike [replaceAll] where the replacement string is always the same.

///

/// The [replace] function is called with the [Match] generated

/// by the pattern, and its result is used as replacement.

///

/// The function defined below converts each word in a string to simplified

/// ‘pig latin’ using [replaceAllMapped]:

/// pigLatin(String words) => words.replaceAllMapped(

/// RegExp(r’\b(\w*?)([aeiou]\w*)', caseSensitive: false),

/// (Match m) => “ m [ 2 ] {m[2]} m[2]{m[1]}${m[1]!.isEmpty ? ‘way’ : ‘ay’}”);

///

/// pigLatin(‘I have a secret now!’); // ‘Iway avehay away ecretsay ownay!’

String replaceAllMapped(Pattern from, String Function(Match match) replace);

replaceRange

作用: 替换固定范围的内容

API源码:

/// Replaces the substring from [start] to [end] with [replacement].

///

/// Creates a new string equivalent to:

/// this.substring(0, start) + replacement + this.substring(end)

/// The [start] and [end] indices must specify a valid range of this string.

/// That is 0 <= start <= end <= this.length.

/// If [end] is null, it defaults to [length].

String replaceRange(int start, int? end, String replacement);

例子:

var str = ‘hello world’;

print(str.replaceRange(6, 11, ‘dart’)); //hello dart

split

作用: 字符串分割成数组

API源码:

/// Splits the string at matches of [pattern] and returns a list of substrings.

///

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数同学面临毕业设计项目选题时,很多人都会感到无从下手,尤其是对于计算机专业的学生来说,选择一个合适的题目尤为重要。因为毕业设计不仅是我们在大学四年学习的一个总结,更是展示自己能力的重要机会。

因此收集整理了一份《2024年计算机毕业设计项目大全》,初衷也很简单,就是希望能够帮助提高效率,同时减轻大家的负担。
img
img
img

既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!

由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频

如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
img

string.

/// That is 0 <= start <= end <= this.length.

/// If [end] is null, it defaults to [length].

String replaceRange(int start, int? end, String replacement);

例子:

var str = ‘hello world’;

print(str.replaceRange(6, 11, ‘dart’)); //hello dart

split

作用: 字符串分割成数组

API源码:

/// Splits the string at matches of [pattern] and returns a list of substrings.

///

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数同学面临毕业设计项目选题时,很多人都会感到无从下手,尤其是对于计算机专业的学生来说,选择一个合适的题目尤为重要。因为毕业设计不仅是我们在大学四年学习的一个总结,更是展示自己能力的重要机会。

因此收集整理了一份《2024年计算机毕业设计项目大全》,初衷也很简单,就是希望能够帮助提高效率,同时减轻大家的负担。
[外链图片转存中…(img-YgJw5enO-1712514344953)]
[外链图片转存中…(img-3j3gyGj0-1712514344954)]
[外链图片转存中…(img-KZy6qVKM-1712514344955)]

既有Java、Web、PHP、也有C、小程序、Python等项目供你选择,真正体系化!

由于项目比较多,这里只是将部分目录截图出来,每个节点里面都包含素材文档、项目源码、讲解视频

如果你觉得这些内容对你有帮助,可以添加VX:vip1024c (备注项目大全获取)
[外链图片转存中…(img-LifeEpIq-1712514344955)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值