LoslegenKostenlos loslegen

Cleaning strings

You've extracted product names from your electronics inventory into a list of strings. The product names are messy, and some have leading and trailing spaces, non-letters, multiple spaces, or mixed cases. Clean the product names from your inventory.

Diese Übung ist Teil des Kurses

Cleaning Data in Java

Kurs anzeigen

Anleitung zur Übung

  • Remove leading and trailing spaces from each product name.
  • Replace multiple spaces with a single space in the products.
  • Convert all product names to lowercase.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

public class ProductDataExample {
	public static void main(String[] args) {
    	String[] messyProducts = {
            "Laptop ", "SMARTPHONE", 
            "Monitor *(Display)*", "   Bluetooth    Speaker   "
        };

        Arrays.stream(messyProducts)
              // Remove leading and trailing spaces
              .map(s -> s.____()
                     .replaceAll("[^a-zA-Z\\s]", "")
                     // Replace multiple spaces with a single space
                     .replaceAll("____", " ")
                     // Convert to lowercase
                     .____())
              .forEach(System.out::println);
    }
}
Code bearbeiten und ausführen