Menu
Coddy logo textTech

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: 10

Check if empty:

String emptyString = "";
bool hasNoChars = emptyString.isEmpty;
print("Is empty string empty? $hasNoChars");
Is empty string empty? true

Check if not empty:

bool hasChars = message.isNotEmpty;
print("Does message have characters? $hasChars");
Does message have characters? true
challenge icon

Challenge

Beginner

Create a program that analyzes a string and provides information about its properties. Your program should:

  1. Read a string input
  2. Check if the string is empty using the isEmpty property
  3. Check if the string is not empty using the isNotEmpty property
  4. Get the length of the string using the length property
  5. 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: true

If the input is an empty string "", your program should output:

String: 
Length: 0
Is Empty: true
Is Not Empty: false

Use 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; // 10

isEmpty - checks if string has no characters:

String emptyString = "";
bool hasNoChars = emptyString.isEmpty; // true

isNotEmpty - checks if string has characters:

bool hasChars = message.isNotEmpty; // true

Try 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
  
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals