我有一个向量向量(没有指定长度)。在此,我想找到一个要搜索的字符串以及它的后续和前置。我已经尝试过了。mainstr 是我转换为字符串的向量。
String mainstr = "[[data link control], [communication, []], [computer, [applications of computer, number of computer]], [world wide web], [lesson, [covered in lesson]], [access to remote], [marketing and sale], [electronic fund transfer], [network, [network of network, wide area network, communication network, computer network, [area network, [local area network, metropolitan area network]]]]]";
String search = "communication network";
if (mainstr.contains(search)) {
if (mainstr.charAt(mainstr.indexOf(search) + search.length()) == ']' && mainstr.charAt(mainstr.indexOf(search) - 2) == '[') {
System.out.println("single term");
} else {
int indexSearch = str.indexOf(search) + search.length();
String followers = str.substring(indexSearch, str.length());
if (!followers.equals("")) {
System.out.println("No FOLLOWERS");
} else {
System.out.println("followers = " + followers.substring(0, followers.indexOf("]")));
}
if (mainstr.charAt(mainstr.indexOf(search) - 4) == ']') {
System.out.println("No pre found");
} else {
System.out.println("preups are present");
String preup = mainstr.split(search)[0].substring(0, mainstr.split(search)[0].length() - 1);
String finalPreup = preup.substring(preup.lastIndexOf("[") + 1, preup.lastIndexOf(","));
System.out.println("final : " + finalPreup);
}
System.out.println("found...");
}
} else {
System.out.println("Not Found");
}
在这种情况下,输出看起来像这样 -
No FOLLOWERS
preups are present
final : network of network, wide area network
found...
我已将此向量转换为字符串,然后我执行了搜索,但我得到了某些字符串的正确输出,在这种指定的情况下,我没有得到所需的输出。我对适用于向量中存在的任何字符串的通用代码感兴趣。提前致谢。
更新::
实际上,这是我放入向量向量中的树结构。
-data link control
-communication
-computer
- applications of computer
-number of computer
-world wide web
-lesson
-covered in lesson
-access to remote
-marketing and sale
-electronic fund transfer
-network
-network of network
-wide area network
-communication network
-computer network
-area network
-local area network
-metropolitan area network
所以我希望搜索将按树进行。例如,如果 search=="wide area network" 那么它的追随者=没有追随者,因为它下面没有层次结构意味着它里面没有元素。及其 Pre up=netwok 因为它是头节点网络的子元素。
提前致谢。