DART中的字符串
在Dart字符串中只不过是字符数组。
字符串有许多方法,允许字符串上的大量操作。
DART定义字符串的语法
type name ='value'; e.g String strname="Welcome in DART!"; or var strname="Welcome in DART!";
- DART 检查字符串是否相等
我们可以检查两个字符串对象(==)表示法。
assert(String1==String2);
- DART字符串连接
我们可以通过String.concat(String其他)连接字符串。
"Welcome".concat(" in Dart");
- 字符串start , end 和 contain方法
我们可以检查字符串是否以某个字符串开始或者结束。或者字符串中是否包含某个子字符串。
var a="Welcome in Dart"; assert(a.startsWith("Welcome")); assert(a.endsWith("Dart")); assert(a.contains("in"));
- 字符串isempty()方法
用于DART检查字符串是否为空
var a=""; a.isEmpty();
- 字符串length属性
用于获取字符串的长度
var a="Welcome"; a.length;
- DART将字符串转换为大小写
string.toUpperCase(); string.toLowerCase();
- 在字符串中替换和修剪方法
DART替换字符串
DART中删除字符串前后的空格:
a.replaceWith(from,to); a.trim();
- 字符串位置方法
我们可以找到字符串位置。
e.indexOf(string,0);
- 字符串replyeAll()
我们可以将字符串字符替换为其他字符。
a.repalceAll("a","d");
DART字符串方法示例
void main() { var a ="Welcome in Dart String"; var c=''; var d="WELCOME"; var e="Welcome in Onitroad Dart"; if(d=="WELCOME"){ print("Compression is True"); } if(a.startsWith("Welcome")){ print("The given string a is start with Welcome: true"); } if(a.endsWith("String")){ print("字符串以String结束"); } if(a.contains("Dart")){ print("字符串中包含Dart"); } if(c.isEmpty()){ print("字符串是空的"); } int len=a.length; print("The length of string a is:${len}"); print("The upper case of string a is"); print(a.toUpperCase()); print("The lower case of string d is"); print(d.toLowerCase()); print("Repalce of Java With Google in string e is:"); e=e.replaceFirst("Java", "Google"); print(e.trim()); int pos=e.indexOf("Dart",0); print("The index of Dart in string e is:${pos}"); print("Repalce all a from d in string a is:"); print(a.replaceAll("a", "d")); print("The last position of E in string d is:"); print(d.lastIndexOf("E")); }
日期:2020-04-11 23:04:00 来源:oir作者:oir