从 Java 中的 array/set 的每个元素中剥离字符
Stripping characters off each element of an array/set in Java
我有一个 HashSet 打印如下:
[Category:Porch of the Cathedral of Palermo, Category:Side views of the Cathedral of Palermo, Category:Church towers of the Cathedral of Palermo, Category:Saint Rosalia by Vincenzo Vitaliano, Category:Piazza del Duomo (Palermo)]
我需要从集合的每个元素中删除 "Category:" 个字符,以生成输出:
[Porch of the Cathedral of Palermo, Side views of the Cathedral of Palermo, Church towers of the Cathedral of Palermo, Saint Rosalia by Vincenzo Vitaliano, Piazza del Duomo (Palermo)]
最简单的方法是什么(Java)?我目前正在使用 HashSet(需要它来确保元素是唯一的)但我可以将它转换为 ArrayList 或任何最适合此任务的东西。
最简单的方法是:将 "Category:"
替换为空字符串。您已经在呼叫 HashSet
的 toString()
。
System.out.println(set.toString().replace("Category:", ""));
输出:
Before: [Category:Side views of the Cathedral of Palermo, Category:Porch of the Cathedral of Palermo]
After: [Side views of the Cathedral of Palermo, Porch of the Cathedral of Palermo]
我有一个 HashSet 打印如下:
[Category:Porch of the Cathedral of Palermo, Category:Side views of the Cathedral of Palermo, Category:Church towers of the Cathedral of Palermo, Category:Saint Rosalia by Vincenzo Vitaliano, Category:Piazza del Duomo (Palermo)]
我需要从集合的每个元素中删除 "Category:" 个字符,以生成输出:
[Porch of the Cathedral of Palermo, Side views of the Cathedral of Palermo, Church towers of the Cathedral of Palermo, Saint Rosalia by Vincenzo Vitaliano, Piazza del Duomo (Palermo)]
最简单的方法是什么(Java)?我目前正在使用 HashSet(需要它来确保元素是唯一的)但我可以将它转换为 ArrayList 或任何最适合此任务的东西。
最简单的方法是:将 "Category:"
替换为空字符串。您已经在呼叫 HashSet
的 toString()
。
System.out.println(set.toString().replace("Category:", ""));
输出:
Before: [Category:Side views of the Cathedral of Palermo, Category:Porch of the Cathedral of Palermo]
After: [Side views of the Cathedral of Palermo, Porch of the Cathedral of Palermo]