从一个字符串值,读一子(Reading a substring from a string valu

2019-10-17 12:04发布

我写了一个Java命令行应用程序的Windows,并存储在一个字符串变量CMD的结果。

我的问题是:是否有可能得到这个变量的子从我所存储的CMD的输出?

我想提出,如果子在CMD输出字符串变量发现,创建一个动作的声明。

下面是应用类的方法:

public static void main(String[] args) throws IOException, InterruptedException
{
    runWndCommand("ping 192.168.11.3");
}

public static void runWndCommand(String cmd) throws IOException, InterruptedException
{
    Runtime runtime = Runtime.getRuntime();
    Process p = runtime.exec(new String[] { "cmd.exe", "/C", cmd });

    Scanner reader = new Scanner(p.getInputStream());

     while (reader.hasNext())
     {
        String r=reader.nextLine();
        System.out.println(r);
     }
     p.waitFor();
 }

Answer 1:

如何使用一个简单的例子contains()在你的例子:

String r = reader.nextLine();

System.out.println(r);

if (r.contains("abc")) {
    System.out.println("abc found");
} else {
    System.out.println("abc not found");
}

这将打印abc found ,如果"abc"是内子r



Answer 2:

考虑java.util.regex.Pattern中它是检验一个更灵活的方式一个字符串是否包含你所期望的



文章来源: Reading a substring from a string value
标签: java cmd