简体   繁体   English

string.indexof(“)”)不起作用

[英]string.indexof(“)”) not working

When i run following code its output is 当我运行以下代码时,其输出为

lcp-   -1  ncp-   -1

I required the following output: 我需要以下输出:

lcp-   8  ncp-   8

Code: 码:

public static void main(String str[]) {
    String fun="M(a,b,c);"; 
    String inputset;
    char m,op;
    fun=fun.substring(0, fun.length()-2);
    //Vector<String> input=new Vector<String>();
    int indx=0;
    String cp;
    cp = ")";
    if(fun.charAt(0)=='M' &&  fun.charAt(1)=='('){
        int lcp=fun.lastIndexOf(cp);
        int ncp=fun.indexOf(cp);
        System.out.println("lcp-   "+lcp+"  ncp-   "+ncp);
    }
}

When I run the code it print -1, -1 for both lcp and nap. 当我运行代码时,它同时为lcp和nap打印-1,-1。 How can I fix it? 我该如何解决?

fun=fun.substring(0, fun.length()-2); will make fun equal to M(a,b,c . So, you will never have ) in the String. fun等于M(a,b,c 。因此,您永远不会在String中拥有) To remove ; 去除; from fun , use fun=fun.substring(0, fun.length()-1); fun ,使用fun=fun.substring(0, fun.length()-1); instead. 代替。

Still you can get answer : lcp- 7 ncp- 7 仍然可以得到答案: lcp- 7 ncp- 7

You need to add 1 before printing in b 在b中打印之前需要加1

System.out.println("lcp-   "+ (lcp+1) +"  ncp-   "+(ncp+1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM