[英]Extracting Strings from a downloaded file
我需要一些幫助來編輯一個字符串並將該部分String添加到字符串的ArrayList
中:
public ArrayList<String> strings = new ArrayList<String>();
基本上我的程序從Internet讀取文件並將讀取文件的內容設置為String。 然后我想通讀字符串內容來找到如下關鍵字:
website=http://google.com
website=http://stackoverflow.com
我只是想將website=
之后的部分添加到字符串數組中,我還想考慮website
部分是否存在,因為除了website=
之外我還會有其他東西website=
例如ping=
。 我只是不知道我會怎么做。
如果有人可以幫我解決這個問題,或者解釋一下更好的方法,我可以自己去做一個洞方法,這對我幫助很大。
所以你把所有這些都放在一個字符串中? 然后正則表達式可能是一種方法,例如Pattern
和Matcher
以及表達式website=(\\S+)
。
例:
String input = "website=http://google.com ping=http://pong.com website=http://stackoverflow.com";
Pattern p = Pattern.compile( "website=(\\S+)" );
Matcher m = p.matcher( input );
while( m.find() )
{
System.out.println(m.group(1));
}
輸出:
http://google.com
http://stackoverflow.com
沒有一種方法,但您可以使用的一些方法是:1。String.contains()函數:使用此方法查看字符串是否包含子字符串。 為了解釋案例敏感性,您可以使用如下:
str = "website="
yourString.toLowerCase().contains(str.toLowerCase())
你可以結合if
邏輯,如:
if(yourString.toLowerCase().contains(str.toLowerCase())
//do something
你可以獲得website=
之后的角色索引:
str="website=";
int index = yourString.indexOf(str)+str.length();
使用以上所有內容為您的需求編寫邏輯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.