Skip to content

String Extension

capitalizeFirstLetter

Capitalizes the first letter of the string.

dart
String example = "flutter and dart";
print(example.capitalizeFirstLetter); // Output: "Flutter and dart"
String example = "flutter and dart";
print(example.capitalizeFirstLetter); // Output: "Flutter and dart"

toPascalCase

Convert the string to Pascal Case.

dart
String example = "flutter and dart";
print(example.toPascalCase); // Output: "FlutterAndDart"
String example = "flutter and dart";
print(example.toPascalCase); // Output: "FlutterAndDart"

toCamelCase

Convert the string to Camel Case.

dart
String example = "flutter and dart";
print(example.toCamelCase); // Output: "flutterAndDart"
String example = "flutter and dart";
print(example.toCamelCase); // Output: "flutterAndDart"

toTitleCase

Convert the string to Title Case.

dart
String example = "flutter and dart";
print(example.toTitleCase); // Output: "Flutter And Dart"
String example = "flutter and dart";
print(example.toTitleCase); // Output: "Flutter And Dart"

toTitle

Similar to toTitleCase but ignores - and _.

dart
String example = "flutter-and-dart";
print(example.toTitle); // Output: "Flutter-And-Dart"
String example = "flutter-and-dart";
print(example.toTitle); // Output: "Flutter-And-Dart"

shouldIgnoreCapitalization

Check if the string should ignore capitalization.

dart
String example = "1flutter";
print(example.shouldIgnoreCapitalization); // Output: true
String example = "1flutter";
print(example.shouldIgnoreCapitalization); // Output: true

removeEmptyLines

Removes empty lines from the string.

dart
String example = "line1\n\nline2";
print(example.removeEmptyLines); // Output: "line1\nline2"
String example = "line1\n\nline2";
print(example.removeEmptyLines); // Output: "line1\nline2"

toOneLine

Convert the string to a single line by replacing newlines with spaces.

dart
String example = "line1\nline2";
print(example.toOneLine); // Output: "line1 line2"
String example = "line1\nline2";
print(example.toOneLine); // Output: "line1 line2"

removeWhiteSpaces

Removes all space from the string.

dart
String example = "  flutter  ";
print(example.removeWhiteSpaces); // Output: "flutter"
String example = "  flutter  ";
print(example.removeWhiteSpaces); // Output: "flutter"

isEmptyOrNull

Check if the string is empty or null.

dart
String? example = null;
print(example.isEmptyOrNull); // Output: true
String? example = null;
print(example.isEmptyOrNull); // Output: true

isNotEmptyOrNull

Check if the string is not empty or null.

dart
String? example = "flutter";
print(example.isNotEmptyOrNull); // Output: true
String? example = "flutter";
print(example.isNotEmptyOrNull); // Output: true

isPalindrom

Check if the string is a palindrome.

dart
String? example = "radar";
print(example.isPalindrom); // Output: true
String? example = "radar";
print(example.isPalindrom); // Output: true

isAlphanumeric

Check if the string is alphanumeric.

dart
String? example = "abc123";
print(example.isAlphanumeric); // Output: true
String? example = "abc123";
print(example.isAlphanumeric); // Output: true

startsWithNumber

Check if the string starts with a number.

dart
String? example = "1flutter";
print(example.startsWithNumber); // Output: true
String? example = "1flutter";
print(example.startsWithNumber); // Output: true

containsDigits

Check if the string contains any digits.

dart
String? example = "abc123";
print(example.containsDigits); // Output: true
String? example = "abc123";
print(example.containsDigits); // Output: true

isValidUsername

Check if the string is a valid username.

dart
String example = "username_123";
print(example.isValidUsername); // Output: true
String example = "username_123";
print(example.isValidUsername); // Output: true

isValidCurrency

Check if the string is a valid currency.

dart
String example = "$100.00 USD";
print(example.isValidCurrency); // Output: true
String example = "$100.00 USD";
print(example.isValidCurrency); // Output: true

isValidPhoneNumber

Check if the string is a valid phone number.

dart
String example = "+1 123-456-7890";
print(example.isValidPhoneNumber); // Output: true
String example = "+1 123-456-7890";
print(example.isValidPhoneNumber); // Output: true

isValidEmail

Check if the string is a valid email address.

dart
String example = "email@example.com";
print(example.isValidEmail); // Output: true
String example = "email@example.com";
print(example.isValidEmail); // Output: true

isValidHTML

Check if the string is an HTML file.

dart
String example = "index.html";
print(example.isValidHTML); // Output: true
String example = "index.html";
print(example.isValidHTML); // Output: true

isValidIp4

Check if the string is a valid IPv4 address.

dart
String example = "192.168.1.1";
print(example.isValidIp4); // Output: true
String example = "192.168.1.1";
print(example.isValidIp4); // Output: true

isValidIp6

Check if the string is a valid IPv6 address.

dart
String example = "FE80::8329";
print(example.isValidIp6); // Output: true
String example = "FE80::8329";
print(example.isValidIp6); // Output: true

isValidUrl

Check if the string is a valid URL.

dart
String example = "https://www.example.com";
print(example.isValidUrl); // Output: true
String example = "https://www.example.com";
print(example.isValidUrl); // Output: true

isValidVideo

Check if the string is a video file.

dart
String example = "video.mp4";
print(example.isValidVideo); // Output: true
String example = "video.mp4";
print(example.isValidVideo); // Output: true

isValidAudio

Check if the string is an audio file.

dart
String example = "audio.mp3";
print(example.isValidAudio); // Output: true
String example = "audio.mp3";
print(example.isValidAudio); // Output: true

isValidImage

Check if the string is an image file.

dart
String example = "image.jpg";
print(example.isValidImage); // Output: true
String example = "image.jpg";
print(example.isValidImage); // Output: true

isValidSVG

Check if the string is an SVG file.

dart
String example = "image.svg";
print(example.isValidSVG); // Output: true
String example = "image.svg";
print(example.isValidSVG); // Output: true

hasMatch

Check if the string matches a given regular expression pattern.

dart
String example = "123";
print(example.hasMatch(r'^\d+$')); // Output: true
String example = "123";
print(example.hasMatch(r'^\d+$')); // Output: true

isNumeric

Check if the string is numeric.

dart
String example = "123";
print(example.isNumeric); // Output: true
String example = "123";
print(example.isNumeric); // Output: true

isAlphabet

Check if the string consists only of alphabets.

dart
String example = "abc";
print(example.isAlphabet); // Output: true
String example = "abc";
print(example.isAlphabet); // Output: true

hasCapitalLetter

Check if the string contains at least one capital letter.

dart
String example = "Abc";
print(example.hasCapitalLetter); // Output: true
String example = "Abc";
print(example.hasCapitalLetter); // Output: true

isBool

Check if the string is a boolean value.

dart
String example = "true";
print(example.isBool); // Output: true
String example = "true";
print(example.isBool); // Output: true

removeWhiteSpaces

Removes all space from the string.

dart
String example = "Hello World";
print(example.removeWhiteSpaces); // Output: HelloWorld
String example = "Hello World";
print(example.removeWhiteSpaces); // Output: HelloWorld

textSize

Returns the size of the text using TextPainter.

dart
String example = "Hello";
print(example.textSize); // Output: Size(width, height)
String example = "Hello";
print(example.textSize); // Output: Size(width, height)

wrapString

Adds a new line to the string after a specified number of words.

dart
String example = "Hi, my name is";
print(example.wrapString(2)); // Output: "Hi, my\nname is"
String example = "Hi, my name is";
print(example.wrapString(2)); // Output: "Hi, my\nname is"

equalsIgnoreCase

Compares two strings for equality, ignoring case.

dart
String example = "Hello";
print(example.equalsIgnoreCase("hello")); // Output: true
String example = "Hello";
print(example.equalsIgnoreCase("hello")); // Output: true

removeSurrounding

Removes a specified delimiter from both ends of the string, if present.

dart
String example = "[Hello]";
print(example.removeSurrounding("[").removeSurrounding("]")); // Output: Hello
String example = "[Hello]";
print(example.removeSurrounding("[").removeSurrounding("]")); // Output: Hello

replaceAfter

Replaces part of the string after the first occurrence of a given delimiter.

dart
String example = "Hello, World";
print(example.replaceAfter(",", " everyone")); // Output: Hello, everyone
String example = "Hello, World";
print(example.replaceAfter(",", " everyone")); // Output: Hello, everyone

replaceBefore

Replaces part of the string before the first occurrence of a given delimiter.

dart
String example = "Hello, World";
print(example.replaceBefore(",", "Hi")); // Output: Hi, World
String example = "Hello, World";
print(example.replaceBefore(",", "Hi")); // Output: Hi, World

anyChar

Returns true if any character in the string matches a given predicate function.

dart
String example = "abc";
print(example.anyChar((char) => char == 'a')); // Output: true
String example = "abc";
print(example.anyChar((char) => char == 'a')); // Output: true

orEmpty

Returns the string if it is not null, or an empty string otherwise.

dart
String? example = null;
print(example.orEmpty); // Output: ""
String? example = null;
print(example.orEmpty); // Output: ""

ifEmpty

Execute a given action if the string is empty.

dart
String example = "";
example.ifEmpty(() => print("String is empty")); // Output: String is empty
String example = "";
example.ifEmpty(() => print("String is empty")); // Output: String is empty

lastIndex

Returns the last character in the string.

dart
String example = "abc";
print(example.lastIndex); // Output: "c"
String example = "abc";
print(example.lastIndex); // Output: "c"

tryToNum, tryToDouble, tryToInt, toNum, toDouble, toInt

Various methods for parsing the string into numbers.

dart
String example = "123";
print(example.tryToNum); // Output: 123
String example = "123";
print(example.tryToNum); // Output: 123

isNotBlank

Check if the string is neither null, empty, nor made solely of whitespace characters.

dart
String example = "  ";
print(example.isNotBlank); // Output: false
String example = "  ";
print(example.isNotBlank); // Output: false

toCharArray

Convert the string to a list of characters.

dart
String example = "abc";
print(example.toCharArray()); // Output: ["a", "b", "c"]
String example = "abc";
print(example.toCharArray()); // Output: ["a", "b", "c"]

insert

Insert a given string at a specified index.

dart
String example = "abc";
print(example.insert(1, "d")); // Output: "adbc"
String example = "abc";
print(example.insert(1, "d")); // Output: "adbc"

isNullOrWhiteSpace

Check if the string is null, empty, or made solely of whitespace characters.

dart
String example = "  ";
print(example.isNullOrWhiteSpace); // Output: true
String example = "  ";
print(example.isNullOrWhiteSpace); // Output: true

limitFromEnd, limitFromStart

Limits the string to a specified length from either the start or the end.

dart
String example = "abcdefgh";
print(example.limitFromEnd(3)); // Output: "fgh"
String example = "abcdefgh";
print(example.limitFromEnd(3)); // Output: "fgh"

asBool

Convert the string to a boolean value based on certain conditions.

dart
String example = "true";
print(example.asBool); // Output: true
String example = "true";
print(example.asBool); // Output: true