ツイートが文字列であると仮定すると、まずすべての「#」を削除してから、すべてのURLをチェックする必要があります。 StringにURLがある場合は、それらを削除する必要があります。
Stringクラスは、String内のStringを他のStringに置き換えるメソッドを提供します。 #
を削除するには、次の操作を行います。
今
//Creating dummy tweet.. you would get it from wherever else
String tweet = "Ronaldo is the #best player in the #world. http://www.google.de";
// Replacing "#" with "" (nothing)
String tweetWithoutHashtag = tweet.replace("#", "");
tweetWithoutHashtag
だけで、不要な#
さんなしで私たちの最初のつぶやきです。
このツイートにURLを見つけるには、Regexの使用をお勧めします。ここで使用するパターンはthisです。
//Create Regex pattern to find urls
Pattern urlPattern = Pattern.compile("(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\[email protected]?^=%&/~+#-])?");
//Create a matcher with our 'urlPattern'
Matcher matcher = urlPattern.matcher(tweetWithoutHashtag);
//Check if matcher finds url
if(matcher.find()) {
//Matcher found urls
//Removing them now..
String tweetWithoutHashtagAndUrl = matcher.replaceAll("");
//Use new tweet here
} else {
//Matcher did not find any urls, which means the 'tweetWithoutHashtag' already is ready for further usage
String tweetWithoutHashtagAndUrl = tweetWithoutHashtag;
}
文字列置換を使用して#を削除し、正規表現を使用してURLを削除する必要があります。正規表現や何かに問題があるときは、新しく質問してください。 – Boendal
[文字列からすべての文字を取り除く]の複製が可能です。(http://stackoverflow.com/questions/4576352/remove-all-occurrences-of-char-from-string) – Berger