Dart/Flutter How to Check string contains alphabetic number

Dart String characters Examples

The string is a group of characters enclosed in single or double-quotes. Characters can be any of the following types

  • alphabetic characters: Each character contains a letter from lowercase - a to z and uppercase - A to Z.
  • `alphanumeric characters: contain alphabetic and numeric characters.
  • Special characters: a character is like %,# ..etc excluding alphabetic and numeric characters.
  • numbers: a character is like numbers are called numeric characters.

In this post, You will learn the following things in Dart.

  • Check String contains alphabetic

  • Check String contains numbers

  • Check String contains alphabetic using a regular expression

  • Check String contains alphanumeric and special characters

How to check if a string contains numbers?

This example checks if a string contains numeric digits in it. The string has a contains method that checks a given string with the matched pattern.

bool contains(Pattern other, [int startIndex = 0])

The pattern is a regular expression created using the RegExp class startIndex is a starting position of a string to search for a pattern.

Here is a dart program to check

void main() {
  var str = "Hello";
  var str1 = "Hello123";

  print(str.contains(new RegExp(r'[0-9]'))); // false
  print(str1.contains(new RegExp(r'[0-9]'))); // true
}

How to check if a string contains number only

This program checks if a given string contains only numbers and no alphabets

void main() {
  var str = "123";
  var str1 = "Hello123";

  print(str.contains(new RegExp(r'^[0-9]+$'))); // true
  print(str1.contains(new RegExp(r'^[0-9]+$'))); // false
}

Example 2: How to check if a string contains alphabetic characters using Regular Expression?

Regular expressions are supported and available in dart using the RegExp standard inbuilt package.

It includes checking regular expressions and pattern matching against a text.

The first regular expression pattern is created using the new RegExp() RegExp is used to check matched string, Regular Expression is used as regexp.

^[a-zA-Z]+

^- Beginning of a string \[ - Character group starting a-z - lower case letter A-Z - upper case letter \] - Character group end \+ - one or more characters

Here is a Program to check String contains Alphabetic using a regular expression

void main() {
  var str = "abc";
  var str1 = "Hello123";

  print(str.contains(new RegExp('^[a-zA-Z]+'))); // true
  print(str1.contains(new RegExp('^[a-zA-Z]+'))); // false
}

Output:

true
false

Example 3- Check String contains Alphanumeric characters or special characters

Regular expression pattern is ^[a-zA-Z0-9_]*$ used.

It returns true for alphabetic, numeric, and underscore characters. return false - for special characters.

Here is a program to check a string for Alphabetic and special characters

void main() {
  var str = "abc_";
  var str1 = "Hello_123";

  print(str.contains(new RegExp('^[a-zA-Z0-9_]*'))); // true
  print(str1.contains(new RegExp('^[a-zA-Z0-9_]*'))); // true
}

When you compile and execute, the output is

true
true

Conclusion

To summarize, You learned multiple programs to check strings containing Alphabetic, numeric and special characters in the Dart language.