gpt4 book ai didi

java - string.indexof (")")不工作

转载 作者:行者123 更新时间:2023-12-01 21:59:39 25 4
gpt4 key购买 nike

当我运行以下代码时,其输出为

lcp-   -1  ncp-   -1

我需要以下输出:

lcp-   8  ncp-   8

代码:

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);
}
}

当我运行代码时,它为 lcp 和 nap 打印 -1, -1。我该如何修复它?

最佳答案

fun=fun.substring(0, fun.length()-2); 将使 fun 等于 M(a,b,c。因此,字符串中永远不会有 ) 。要从 fun 中删除 ;,请改用 fun=fun.substring(0, fun.length()-1);

关于java - string.indexof (")")不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33795726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com