-3
我在Java中如何從Java中的字符串中刪除重複項?
String s1 = "Chicago/London/London/London/Paris";
String s2 = "London/London/London/Paris";
String s3 = "Prague/London/London/London";
String s4 = "London/London";
這些字符串我應該在的replaceAll功能使用哪個正則表達式來從做「芝加哥/倫敦/巴黎」,「倫敦/巴黎」,「布拉格/倫敦」和「倫敦」這些字符串?非常感謝你
你嘗試過什麼做的?爲什麼你認爲正則表達式是正確的工具呢? – UnholySheep
從它看起來,Java String不是存儲它的最佳方式。一個想法是分割它,把它放到一個Set中,從而自動消除重複項,並且,如果你真的需要這個作爲一個字符串,使用'/'再次加入它。 –
這樣的事情會做到這一點: new ArrayList <>(new HashSet <>(Arrays.asList(s1.split(「/」)))) – ronhash