我有一個這樣的字符串數組: String tweetString = ExudeData.getInstance().filterStoppingsKeepDuplicates(tweets.text); // get array of words and split String[] wordArray = tweetString.split(" ");拆分數組后,我打印以下內容:System.out.println(Arrays.toString(wordArray));我得到的輸出是:[new, single, fallin, dropping, days, artwork, hueshq, production, iseedaviddrums, amp, bigearl7, mix, reallygoldsmith, https, , , t, co, dk5xl4cicm, https, , , t, co, rvqkum0dk7]我想要的是刪除逗號,https和單個字母(如“t”)的所有實例(使用上述方法后)。所以我想以這個結束:split[new, single, fallin, dropping, days, artwork, hueshq, production, iseedaviddrums, amp, bigearl7, mix, reallygoldsmith, co, dk5xl4cicm, https, co, rvqkum0dk7]我嘗試過做替換所有像這樣:String sanitizedString = wordArray.replaceAll("\\s+", " ").replaceAll(",+", ",");但這只是給了我相同的初始輸出,沒有變化。有什么想法嗎?
3 回答

九州編程
TA貢獻1785條經驗 獲得超4個贊
如果您使用的是 Java 8
String[] result = Arrays.stream(tweetString.split("\\s+")) .filter(s -> !s.isEmpty()) .toArray(String[]::new);
我想要的是刪除逗號,https和單個字母(如“t”)的所有實例
在這種情況下,您可以制作多個過濾器,例如@Andronicus這樣做或與匹配和一些正則表達式,如下所示:
String[] result = Arrays.stream(tweetString.split("\\s+")) .filter(s -> !s.matches("https|.|\\s+")) .toArray(String[]::new);

有只小跳蛙
TA貢獻1824條經驗 獲得超8個贊
你可以做這樣的事情:
String[] filtered = Arrays .stream(tweetString.split("[ ,]")) .filter(str -> str.length() > 1) .filter(str -> !str.equals("http"))

阿晨1998
TA貢獻2037條經驗 獲得超6個贊
根據我的評論,這里是快速解決方案。(使用所有關鍵字增強正則表達式)
private static void replaceFromRegex(final String text ) { String result = text.replaceAll("https($|\\s)| (?<!\\S)[^ ](?!\\S)",""); System.out.println(result); }
然后測試
public static void main(String []args) throws Exception{ replaceFromRegex("new single fallin dropping, , https"); }
注意:這只是示例,您必須增強正則表達式以考慮起始單詞(例如,以https開頭的字符串,然后是空格等)
添加回答
舉報
0/150
提交
取消