Flutter | 如何检查字符串中是否不包含子字符串或字符

5

我正在学习Flutter/Dart编程语言,我想要进行以下检查 - 验证用户输入的电子邮件地址是否包含“@”符号

String myString = 'Dart';

// just like i can do the following check
myString.contains('a') ? print('validate') : print('does not validate')

// I want to do another check here
myString does not contain('@') ? print('does not validate'): print('validate')

请问是否有内置函数可以完成这样的事情。

1个回答

8

只需简单地在开头加上非!运算符(在空安全中也称为bang运算符)

void main() {
  String myString = 'Dart';

// just like i can do the following check
  myString.contains('a') ? print('validate') : print('does not validate');

// I want to do another check here
  !myString.contains('@') ? print('does not validate') : print('validate');
}


我不知道有任何内置函数可以做到这一点。 - Yeasin Sheikh
好的!所以我可以在几乎所有东西中使用这个 bang 运算符。 - samarth
bool check = false; !check ? print('getting the hang of it') : print('not yet'); - samarth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接