Java;从字符串中删除字符时,循环意外停止

时间:2019-03-02 16:29:51

标签: java string for-loop char

我想编写一个从字符串(stringbuilder)中删除字符并继续执行直到循环结束的代码。当我删除字符串中的字符时,包含此字符串的循环将立即停止。

      for(i=1;i<n;i++){
            for(j=0;j<x[i].length();j++){
                if((x[i].contains(v)) || (x[i].contains(other))){
                }
                else{

                    for(m=0;m<u;m++){
                    for(l=0;l<x[i].length();l++){

                        System.out.format("d[i][l]%s  charat(m)%s  \n",d[i][l],v.charAt(m));

                        if(d[i][l] != v.charAt(m)){

                            count++;
                            System.out.println(m);
                            System.out.format("count:%d\n",count);

                        }
                        if(count == x[i].length()){

                            v.deleteCharAt(m);
                            others.deleteCharAt(v.length()-m-1);
                            System.out.format("count%d \n",count);
                            System.out.format("u%d ",u);
                            System.out.println(v);
                             count=0;
                        }
                    }
                   count=0;
                    }
                }
                }
            }

1 个答案:

答案 0 :(得分:0)

这是从给定字符串中删除给定字符的代码。

import java.util.*;
class Test{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter String : ");
        String str=sc.nextLine();
        System.out.print("\nEnter No.of Deletion : ");
        int n=sc.nextInt();
        //convert String  char Array
        char[] st = str.toCharArray();

        if(st.length>n)
        {
            for(int i=0;i<n;i++)
            {
                st[i]='\0';
            }
        }
        else{
            for(int j=0;j<st.length;j++)
            {
                st[j]='\0';
            }
        }
        System.out.println("Rest String : "+String.valueOf(st));

    }
}
相关问题