簡體   English   中英

如何重新格式化推文,始終用轉義的引號替換單引號

[英]How to reformat tweets, replacing single quotes with escaped quotes consistently

當前,我有一個方法,將在下面顯示,該方法旨在刪除使用twitter API檢索的推文的所有單引號和換行符。 換行符每次都可以使用,但是盡管如此,由於某些原因,替換字符只能使用一半時間。 這種替換方法在寫入文件之前恰好稱為一行,因此我懷疑由於某種原因它會丟失迭代。 這些推文似乎是隨機過濾的。 我無法解釋為什么有時會起作用。 奇怪的音符。 用x.replace(“ \\”,“ \\\\'”);刪除if語句; 導致沒有任何內容被過濾。

提前致謝。

public static String replace(String x) {
    String replaced = x;


        if (x.contains("'")) {
            replaced = x.replaceAll("'", "\\\\'");
        }
        if(x.contains("\n") || x.contains("\r")){
            replaced = x.replaceAll("\\r\\n|\\r|\\n", " ");
        }

        System.out.println(replaced);

    return replaced;
}

編輯:查看它,如果語句激活,但是在少數情況下,一些推文很簡單地轉到replaceAll行,並且不會被替換。 為什么不? 我沒有任何線索。

示例數據: https : //justpaste.it/15c6t第一個問題是“ You're”第20行。

似乎在某些情況下,第一種替換方法會干擾第二種替換方法。 當將兩者分為兩種不同的方法(雖然笨拙)時,它應該發揮應有的作用。

public static String replace(String x) { //Cleans the single quotes
    String replaced = x;
        if (replaced.contains("'")) {
            replaced = x.replaceAll("'", "\\\\'");             
        }
    return replaced;
}

public static String removeEnters(String x){ //Removes any enters
    String replaced = x;
    if(replaced.contains("\n") || x.contains("\r")){
            replaced = x.replaceAll("\\r\\n|\\r|\\n", " ");
    }
    return replaced;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM