gpt4 book ai didi

java - 从Java中的数组/集合的每个元素中剥离字符

转载 作者:行者123 更新时间:2023-11-30 10:52:09 25 4
gpt4 key购买 nike

我有一个打印如下的 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:" 替换为空字符串。您已经在调用 HashSettoString()

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]

关于java - 从Java中的数组/集合的每个元素中剥离字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34460233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com