文字列のクリーニング
電子機器の在庫から製品名を文字列のリストとして抽出しました。製品名はいくつか問題があり、先頭や末尾のスペース、英字以外の文字、複数の連続するスペース、大文字と小文字の混在などが含まれています。在庫内の製品名をクリーニングしましょう。
この演習はコースの一部です
Java によるデータクリーニング
演習の手順
- 各製品名の先頭と末尾のスペースを削除しましょう。
- 複数の連続するスペースを1つのスペースに置換しましょう。
- すべての製品名を小文字に変換しましょう。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
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);
}
}