dart 内置数据类型 String

本文深入讲解Dart语言中字符串的创建、操作及常见方法。包括使用单引号、双引号、多行字符串、字符串插值、表达式插入、字符串比较、长度获取、判断空值、字符访问以及字符串连接和重复等操作。

先看来自官网的解释:

A Dart string is a sequence of UTF-16 code units. You can use either single or double quotes to create a string

从上面的文档中发现创建字符串可以使用单引号或者双引号 这点和JavaScript和groovy kotilin一样

例子:

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.";

除此之外我们还可以使用多行,创建多行字符串的另一种方法是:使用带有单引号或双引号的三重引号

void main(){
  var s1 = '''
You can create
multi-line strings like this one.
''';
  print(s1);
}

打印结果:

You can create
multi-line strings like this one.

除了三个单引号。还可以使用三个双引号

void main(){
  var s2 = """This is also a
multi-line string.""";
  print(s2);
}

可以在前面加r   表示原始字符串

void main(){
  var s = r'In a raw string, not even \n gets special treatment.';
  print(s);
}

结果:

In a raw string, not even \n gets special treatment.

dart支持表达式

You can put the value of an expression inside a string by using ${expression}. If the expression is an identifier, you can skip the {}. To get the string corresponding to an object, Dart calls the object’s toString() method

翻译:

可以使用${expression}将表达式的值放在字符串中。如果表达式是标识符,可以跳过{}。要获得与对象对应的字符串,Dart调用对象的toString()方法

例子:

void main(){
  var s = 'string interpolation';
  print('Dart has $s, which is very handy.');
}

结果:

Dart has string interpolation, which is very handy.

这个是省略了{}

比较二个字符串相等时使用==

Note: The == operator tests whether two objects are equivalent. Two strings are equivalent if they contain the same sequence of code units

翻译:

==操作符测试两个对象是否相等。如果两个字符串包含相同的代码单元序列,则它们是等价的

 

下面介绍String一些常用的方法

官方文档:String 对象api

1:获取字符串的长度

length

2:判断是否为空

isEmpty() 还有一个isNotEmpty()判断是否不为空

对着Java中的String去学就很简单了,没什么区别,但是如果想获取字符串中的某一个字符的话 有一个简单的方法

void main(){
  String s1 = 'eeaabbbbb';
  print(s1[1]);
}

这打印出来就是e了,

最后还有一个关于字符串的操作符

Operators

operator *(int times) → String

Creates a new string by concatenating this string with itself a number of times. [...]

operator +(String other) → String

Creates a new string by concatenating this string with other[...]

operator ==(Object other) → bool

Returns true if other is a String with the same sequence of code units. [...]

override

operator [](int index) → String

Gets the character (as a single-code-unit String) at the given index[...]

 

 *号操作符 表示后面累计添加多少个以前的字符串

void main(){
  String s1 = 'pig';
  print(s1*5);
}

结果:

pigpigpigpigpig

如果s1*0的话得到的是一个空字符串

+号;链接符

==号表示赋值

[]表示获取字符串中某个字符

<think>好的,用户想了解Dart编程语言的内置库的官方文档和功能列表。首先,我需要回忆一下Dart内置库有哪些,以及它们的核心功能。根据引用[2]和引用[4],Dart内置库包括核心类型、集合、异步处理、数学计算、文件I/O等。 接下来,用户可能需要知道这些库的具体用途和示例,比如dart:core处理基本类型和常用功能,dart:async用于异步编程。我需要确保每个库的功能描述准确,并引用正确的参考资料。比如,引用[2]提到核心库涵盖内置类型和集合,引用[4]说明库的引入方式。 然后,用户可能想知道如何访问官方文档,应该直接提供链接到Dart官网的库概览页面。同时,用户可能对实际应用场景感兴趣,比如Flutter开发中如何使用这些库,可以结合引用[1]提到的Dart在Flutter中的使用情况。 还要注意用户是否分不清内置库和第三方库,需要明确区分。此外,可能需要提到一些常用库的具体函数或类,如Future和Stream来自dart:async,math库的数学函数等,以增强实用性。 最后,生成相关问题时要围绕内置库的扩展学习、异步编程、集合操作以及数学库的应用,帮助用户进一步探索。需要确保所有信息准确,引用正确的来源,比如引用[2]和引用[4]中的相关内容。</think>### Dart内置库功能概览 Dart的标准库提供了丰富的内置功能模块,通过`dart:`前缀的库可直接导入使用。以下是主要内置库的功能列表及典型应用场景: 1. **dart:core** - 包含基础类型(`int`、`String`、`bool`等)和核心API - 常用工具类:`DateTime`、`RegExp`、`Uri` - 示例:`String str = 'Dart'.padLeft(10);`[^2] 2. **dart:async** - 异步编程支持:`Future`、`Stream`、`Timer` - 用于I/O操作和事件处理 - 示例: ```dart Future.delayed(Duration(seconds: 1), () => print('延迟执行')); ``` 3. **dart:collection** - 增强型集合类:`Queue`、`LinkedList`、`HashMap` - 提供集合工具方法:`Equality`、`UnmodifiableListView` 4. **dart:convert** - 编解码支持:JSON、UTF-8、Base64 - 示例:`jsonEncode({'name': 'Dart'});`[^2] 5. **dart:math** - 数学计算:三角函数、随机数、常量(`pi`、`e`) - 示例:`double root = sqrt(9);`[^2] 6. **dart:io** - 文件系统操作(非Web环境) - 网络通信:`HttpServer`、`Socket` 7. **dart:typed_data** - 处理字节数据的类型化列表:`ByteBuffer`、`Int32List` $$ \text{数学公式示例:} \quad \sum_{i=1}^n i = \frac{n(n+1)}{2} $$ ### 官方文档访问 Dart官方库文档可通过以下路径查看: https://dart.dev/guides/libraries/library-tour https://api.dart.dev/stable/dart-core/dart-core-library.html
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值