You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter/Dart如何检查字符串不包含指定子串/字符 实现邮箱校验

Dart标准库中没有提供单独的反向包含判断内置方法,你可以直接对String.contains()的返回值使用逻辑非运算符!取反,即可实现「判断字符串是否不包含指定内容」的需求。

对应你的业务场景实现代码如下:

String myString = 'Dart';

// 原有正向包含校验
myString.contains('a') ? print('validate') : print('does not validate');

// 反向判断是否不包含@的实现
!myString.contains('@') ? print('does not validate') : print('validate');

针对邮箱校验场景,也可以直接用正向判断简化逻辑,避免多余的取反操作:

String inputEmail = '用户输入的邮箱地址';
if (inputEmail.contains('@')) {
  print('校验通过');
} else {
  print('邮箱必须包含@符号,校验不通过');
}

内容的提问来源于stack exchange,提问作者samarth

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 19:15:03