的標題我的意思是公共類的聲明總是相同的,但名稱有所不同:在常量字符串中查找變化字符串的最快算法/方法?
public class <class name>
我想搜索Java源代碼(在JTextArea)爲公共類。
所以首先它會發現:
public class Example1
或public class Example2
或public class Example3
等。
然後我想存儲字符串Example1
或Example2
或Example3
這個程序會用Java編寫。
誰能幫助我嗎?
UPDATE:
有一點點去,到Pattern和Matcher類很新。
private String findPublicClass() {
Pattern pattern = Pattern.compile("\\s*public\\s+class\\s+(\\w+)");
Matcher matcher = pattern.matcher(txtSource.getText());
String s = null;
boolean found = false;
while (matcher.find()) {
s = matcher.group(1);
found = true;
}
if(!found)
System.out.println("No public class found.");
return s;
}
到目前爲止你有寫過任何代碼嗎?如果沒有,我建議給它一個最好的鏡頭,然後用一些示例代碼回到這裏,這樣我們可以更好地幫助你,所以我們知道你至少嘗試過。 – Franklin
@Franklin我有一點點去。 – Ciphor