शुरू करेंमुफ़्त में शुरू करें

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.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Cleaning Data in Java

पाठ्यक्रम देखें

अभ्यास निर्देश

  • 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.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

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);
    }
}
कोड संपादित करें और चलाएँ