- 我有一個字符串,它具有下面的值。我需要從中獲取函數名稱。並且函數名稱是動態的。
使用下面的代碼,我可以得到單詞「功能」已經發生了多少次。不知道如何獲取函數名稱。使用java從另一個字符串獲取特定字符串
String strLine=request.getParameter("part1"); if(strLine!=null){ String findStr = "function "; int lastIndex = 0; int count =0; while(lastIndex != -1){ lastIndex = strLine.indexOf(findStr,lastIndex); if(lastIndex != -1){ count ++; lastIndex+=findStr.length(); } } System.out.println("count "+count); }
part1是來自用戶的值。它可以是,
function hello(){ } function here(){ }
在上面的事情中,沒有函數和函數名稱被改變。
我想得到,hello()和here()作爲輸出。
0
A
回答
0
@ Bobby rachel。對不起,我不明白你的問題。 但是如果你想檢索名字,你可以使用XML格式。然後從中檢索。
例如 String userid = request.getParameter(「part1」);
String stri = "req=<header><requesttype>LOGIN</requesttype></header>"
+ "<loginId>"
+ userid //the string you get and want to retrieve
+ "</loginId>" //from this whole string
object.searchIn(字符串登錄ID)//輸入名稱齊名的要檢索
另一個函數來獲取用戶ID
公共字符串serachIn(字符串searchNode)的值{ 嘗試{
int firstpos = stri.indexOf("<" + searchNode + ">");
int endpos = stri.indexOf("</" + searchNode + ">");
String nodeValue = stri.substring(firstpos + searchNode.length() + 2, endpos);
System.out.println("node value"+searchNode+nodeValue);
return nodeValue;
}
我希望它能幫助
2
如果我已經理解了你的問題,你試着解析字符串part1,並且你想獲得函數名。它們是動態的,因此您不能對名稱做任何假設。在這種情況下,您必須編寫自己的解析器或使用正則表達式。
下面的程序提取使用正則表達式的函數名:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Stackoverflow {
private static Pattern pattern = Pattern.compile("\\s([a-zA-Z0-9_]+\\(\\))", Pattern.DOTALL | Pattern.MULTILINE);
public static void main(String[] args) {
String part1 = "function hello(){\n" +
" }\n" +
" function here(){\n" +
" }";
Matcher matcher = pattern.matcher(part1);
while (matcher.find()) {
String str = matcher.group();
System.out.println(str);
}
}
}
輸出是:
hello()
here()
我希望這回答了你的問題。
0
可以使用regex
實現這一點,這裏有一個例子:
public static List<String> extractFunctionsNames(String input) {
List<String> output = new ArrayList<String>();
Pattern pattern = Pattern.compile("(function\\s+([^\\(\\)]+\\([^\\)]*\\)))+");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
output.add(matcher.group(2));
}
return output;
}
public static void main(String[] args) {
String input = "function hello(){\n"
+ " \n}"
+ "\nfunction here(){\n"
+ "}\n";
System.out.println(extractFunctionsNames(input));
}
OUTPUT:
[hello(), here()]
請注意,此代碼是不可靠的,因爲像function hello() {print("another function test()")}
一個輸入將輸出[hello(), test()]
相關問題
- 1. 從字符串獲取特定字符
- 2. 從另一字符串中提取字符串兩個特定字符
- 3. 獲取特定部分從一個字符串到另一個
- 4. 使用PHP從另一個字符串中提取字符串
- 5. 如何從另一個字符串獲取字符串值?
- 6. 如何從另一個字符串獲取字符串值
- 7. 在java中使用字符串:提取特定字符串
- 8. 從字符串複製特定字符到另一個字符串
- 9. 獲取字符串的特定字符
- 10. 如何從另一個字符串使用PHP獲取某些字符串?
- 11. Java/XML:在另一個字符串中使用字符串
- 12. 在特定字符串後獲取下一個字符/字符串
- 13. 如何從字符串(url)獲取特定的字符串?
- 14. 搜索特定字符串在另一個字符串
- 15. 如何從字符串使用PHP獲取特定的子字符串數字?
- 16. 如何獲取另一個字符串內的字符串之間的特定字符串?
- 17. 如何從一個字符串獲取特定的字符串在PHP
- 18. 字符串使用java在字符串中獲取特定值的操作
- 19. 獲取另一個字符串右側的字符串
- 20. 從特定字符串獲取金額
- 21. 獲取從包含特定字符串
- 22. 從字符串獲取特定值
- 23. 從NSString獲取特定的字符串
- 24. 從另一個url或字符串中提取字符串
- 25. 如何從另一個字符串中提取字符串php
- 26. 從另一個字符串中提取字符串
- 27. 使用curl獲取字符串並從字符串中查找特定值
- 28. 將一個字符串到另一個字符串中的Java
- 29. 從一個字符串獲取幾個子字符串
- 30. 從字符串獲取子字符串__
什麼是關於'function hello(){print(「hi from function hello」)}'? – kan