String Properties
Part of the Fundamentals section of Coddy's Dart journey — lesson 40 of 94.
String properties provide information about strings. Key properties: length, isEmpty, and isNotEmpty.
Create a string:
void main() {
String message = "Hello Dart";
}Get character count:
int charCount = message.length;
print("Character count: $charCount");Character count: 10Check if empty:
String emptyString = "";
bool hasNoChars = emptyString.isEmpty;
print("Is empty string empty? $hasNoChars");Is empty string empty? trueCheck if not empty:
bool hasChars = message.isNotEmpty;
print("Does message have characters? $hasChars");Does message have characters? trueChallenge
BeginnerCreate a program that analyzes a string and provides information about its properties. Your program should:
- Read a string input
- Check if the string is empty using the
isEmptyproperty - Check if the string is not empty using the
isNotEmptyproperty - Get the length of the string using the
lengthproperty - Print the results in the exact format shown below
For example, if the input is "Dart", your program should output:
String: Dart
Length: 4
Is Empty: false
Is Not Empty: trueIf the input is an empty string "", your program should output:
String:
Length: 0
Is Empty: true
Is Not Empty: falseUse string properties (length, isEmpty, isNotEmpty) to complete this task.
Cheat sheet
String properties provide information about strings in Dart:
length - returns the number of characters:
String message = "Hello Dart";
int charCount = message.length; // 10isEmpty - checks if string has no characters:
String emptyString = "";
bool hasNoChars = emptyString.isEmpty; // trueisNotEmpty - checks if string has characters:
bool hasChars = message.isNotEmpty; // trueTry it yourself
import 'dart:io';
void main() {
// Read the input string
String input = stdin.readLineSync() ?? "";
// TODO: Check if the string is empty using isEmpty property
// TODO: Check if the string is not empty using isNotEmpty property
// TODO: Get the length of the string using length property
// TODO: Print the results in the required format
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number