查找在向量中指定字符串之前和之前发生的字符串

时间:2013-07-11 08:02:11

标签: java string vector

我有一个向量向量(没有指定的长度)。在这里我想找到一个要搜索的字符串以及它的后续和预备。 我已经尝试过这么做了。 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

所以我希望搜索将按照树。 例如如果搜索==“广域网”,那么其关注者=没有粉丝,因为它没有下面的hirarchy意味着里面没有元素。并且它的Pre up = netwok因为它是头节点网络的子元素。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我相信你的代码没有输出你想要的东西,因为你需要找到[的倒数第二个索引,而不是最后一个索引。而且,一旦修复,我认为您的代码不会跳过带有子节点的节点。为此,我建议使用支架计数器进行while循环。

以下是我的尝试。它只是一个基本程序 - 没有边界检查等,但它应该让你知道该怎么做。

  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";
  int start = mainstr.indexOf(search);
  if (start != -1)
  {
     int end = start + search.length();
     int count = 0;
     int pos = end;
     if (mainstr.charAt(end+2) == '[')
     {
        while (count != -1)
           if (mainstr.charAt(++pos) == ']')
              count--;
           else if (mainstr.charAt(++pos) == '[')
              count++;
        System.out.println("Ancestors = " + mainstr.substring(end+2, pos-1));
     }
     count = 0;
     pos = start;
     int lastComma = -1;
     while (count != 2)
        switch (mainstr.charAt(--pos))
        {
           case ']': count--; break;
           case '[': count++; break;
           case ',': lastComma = pos;
        }
     System.out.println("Parent = " + mainstr.substring(pos+1, lastComma));
  }

当您正在寻找一个节点时,这是一个问题,该节点是字符串中较早出现的另一个节点的子字符串。

为此,我认为正则表达式可能很好用:

替换

int start = mainstr.indexOf(search);
if (start != -1)
{

Pattern p = Pattern.compile("(?:^|, |\\[)(" + search + ")(?:]|, |$)");
Matcher m = p.matcher(mainstr);
if (m.find())
{
   int start = m.start(1);

Test

Java regex reference