Dart语言教程:字符串属性与处理方法

2019-06-2215:05:07编程语言入门到精通Comments5,084 views字数 1708阅读模式

Dart字符串数据类型表示一系列字符。Dart字符串是一系列UTF 16代码单元。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

Dart中的字符串值可以使用单引号或双引号或三引号表示。单行字符串使用单引号或双引号表示。三引号用于表示多行字符串。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

在Dart中表示字符串值的语法如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

语法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

String  variable_name = 'value'  

// 或者 
String  variable_name = ''value''  

// 或者 
String  variable_name = '''line1 
line2'''  

// 或者 
String  variable_name= ''''''line1 
line2''''''
Dart

以下示例演示了如何在Dart中使用String数据类型。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

void main() { 
   String str1 = 'this is a single line string'; 
   String str2 = "this is a single line string"; 
   String str3 = '''this is a multiline line string'''; 
   String str4 = """this is a multiline line string"""; 

   print(str1);
   print(str2); 
   print(str3); 
   print(str4); 
}
Dart

它将产生以下输出 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

this is a single line string 
this is a single line string 
this is a multiline line string 
this is a multiline line string
Shell

字符串是不可变的。但是,字符串可以进行各种操作,结果字符串可以存储为新值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

字符串插值

通过将值附加到静态字符串来创建新字符串的过程称为连接或插值。它是将字符串添加到另一个字符串的过程。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

运算符加运算符(+)是连接/插入字符串的常用机制。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

示例1文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

void main() { 
   String str1 = "hello"; 
   String str2 = "world"; 
   String res = str1+str2; 

   print("The concatenated string : ${res}"); 
}
Dart

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

The concatenated string : Helloworld
Shell

示例2文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

可以使用${}来插入字符串中Dart表达式的值。如下示例 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

void main() { 
   int n=1+1; 

   String str1 = "The sum of 1 and 1 is ${n}"; 
   print(str1); 

   String str2 = "The sum of 2 and 2 is ${2+2}"; 
   print(str2); 
}
Dart

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

The sum of 1 and 1 is 2 
The sum of 2 and 2 is 4
Shell

字符串属性

下表中列出的字符串属性都是只读的。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

编号属性描述
1codeUnits返回此字符串的UTF-16代码单元的不可修改列表。
2isEmpty如果此字符串为空,则返回true
3length返回字符串的长度,包括空格,制表符和换行符。

操纵字符串的方法

Dart语言的core库中的String类还提供了操作字符串的方法。其中一些方法如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

编号属性描述
1toLowerCase()将此字符串中的所有字符转换为小写。
2toUpperCase()将此字符串中的所有字符转换为大写。
3trim()返回没有任何前导和尾随空格的字符串。
4compareTo()将此对象与另一对象进行比较。
5replaceAll()用给定值替换与指定模式匹配的所有子字符串。
6split()在指定分隔符的匹配处拆分字符串并返回子字符串列表。
7substring()返回此字符串的子字符串,字符串从startIndex(包括)延伸到endIndexexclusive
8toString()返回此对象的字符串表示形式。
9codeUnitAt()返回给定索引处的16位UTF-16代码单元。

原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/dart/dart_programming_string.html#article-start文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13746.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/13746.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定