Menu
CoddyTech

Proprietà delle stringhe

Fa parte della sezione Fondamenti del percorso Dart di Coddy. Lezione 40 di 94.

Le proprietà delle stringhe forniscono informazioni sulle stringhe. Proprietà principali: length, isEmpty e isNotEmpty.

Crea una stringa:

void main() {
  String message = "Hello Dart";
}

Ottieni il conteggio dei caratteri:

int charCount = message.length;
print("Character count: $charCount");
Character count: 10

Verifica se è vuota:

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

Verifica se non è vuoto:

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

Sfida

Principiante

Crea un programma che analizzi una stringa e fornisca informazioni sulle sue proprietà. Il tuo programma dovrebbe:

  1. Leggere una stringa in input
  2. Controllare se la stringa è vuota usando la proprietà isEmpty
  3. Controllare se la stringa non è vuota usando la proprietà isNotEmpty
  4. Ottenere la lunghezza della stringa usando la proprietà length
  5. Stampare i risultati nel formato esatto mostrato qui sotto

Ad esempio, se l'input è "Dart", il tuo programma dovrebbe produrre:

String: Dart
Length: 4
Is Empty: false
Is Not Empty: true

Se l'input è una stringa vuota "", il tuo programma dovrebbe produrre:

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

Usa le proprietà delle stringhe (length, isEmpty, isNotEmpty) per completare questa attività.

Provalo tu

import 'dart:io';

void main() {
  // Leggi la stringa di input
  String input = stdin.readLineSync() ?? "";
  
  // TODO: Controlla se la stringa è vuota usando la proprietà isEmpty
  
  // TODO: Controlla se la stringa non è vuota usando la proprietà isNotEmpty
  
  // TODO: Ottieni la lunghezza della stringa usando la proprietà length
  
  // TODO: Stampa i risultati nel formato richiesto
  
}
quiz iconMettiti alla prova

Questa lezione include un breve quiz. Inizia la lezione per rispondere e tenere traccia dei tuoi progressi.

Tutte le lezioni di Fondamenti

Esercitati da solo: Compilatore Dart online