From d704ec1faba91ffd6bea45bad18d4a9aa6ea8de4 Mon Sep 17 00:00:00 2001 From: Nipun Shah Date: Mon, 20 Jun 2022 23:42:07 +0530 Subject: [PATCH] Feature : new methods for string - digits, letters, alphanumeric --- lib/src/string.dart | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/src/string.dart b/lib/src/string.dart index 050d589..cb62a7f 100644 --- a/lib/src/string.dart +++ b/lib/src/string.dart @@ -295,3 +295,35 @@ String buildString(void Function(StringBuffer sb) builderAction) { builderAction(buffer); return buffer.toString(); } + +extension StringIsLetterOrDigits on String { + /// Returns `true` if all the characters in string are digits & there is atleast 1 character, + /// `false` elseways + bool get isDigit { + if (isBlank) { + return false; + } + final regex = RegExp(r'^\d+$'); + return regex.hasMatch(this); + } + + /// Returns `true` if all the characters in string are letters & there is atleast 1 character, + /// `false` elseways + bool get isAlpha { + if (isBlank) { + return false; + } + final regex = RegExp(r'^[a-zA-Z]+$'); + return regex.hasMatch(this); + } + + /// Returns `true` if all the characters in string are either digits or letters + /// & there is atleast 1 character, `false` elseways + bool get isAlNum { + if (isBlank) { + return false; + } + final regex = RegExp(r'^[a-zA-Z0-9]+$'); + return regex.hasMatch(this); + } +}