我在以後使用Twitter API時將以下代碼用作測試。JAVA String replaceAll#標籤內容
當我從我的流中獲取推文並將它們存儲在mySQL中時,如何刪除與任何結尾字符一起的hashtags? (基本省略hashtag中的所有內容)
我能夠使用replaceAll並傳遞「#」和「,」值並將它們替換爲「」。它的工作原理和輸出低於代碼,但我將如何使用來替換hashtag的所有內容,包括「not」「#not」?
public class replaceAllTest {
public static void main (String args[]){
String sampleString = "This game was amazing, #not";
System.out.println("Before: " + sampleString);
sampleString = sampleString.replaceAll("#", "");
sampleString = sampleString.replaceAll(",", "");
System.out.println("After: " + sampleString);
}
}
從上面的代碼的輸出:
Before: This game was amazing, #not
After: This game was amazing not
從上面的代碼預期輸出
:
Before: This game was amazing, #not
After: This game was amazing
任何幫助,將不勝感激,謝謝Z19
我建議你查一下正則表達式的一些入門教程。 '(,?\ s *#[^ \ s] +)+'http://www.rexfiddle.net/r3hU8jT –
謝謝你們三位的幫助。我感謝你的快速回復:) – Z19