繁体   English   中英

如何在Java中没有任何内置方法的情况下从字符串中删除字符我不想使用stringbuilder或字符串缓冲区

[英]How to remove character from string without any built in method in Java neither I want to use stringbuilder or string buffer

谁能告诉我如何在不使用任何内置函数和 stringbuffer 或构建器的情况下从字符串中删除字符。 虽然我试图形成一个逻辑但没有得到答案。 在下面的代码中,我想从输入字符串中删除“他”。

import java.util.Scanner;
public class p1 {
    public static void main(String[] rgs) {
        System.out.println("enter the string");
        Scanner s = new Scanner(System. in );
        String str = s.nextLine();
        p1 g = new p1();
        g.replacechar(str);
    }
    public void replacechar(String str) {
        String e = "";
        int l = str.length();
        char ch[] = str.toCharArray();
        for (int i = 0; i < l; i++) {
            if (ch[i] == 'h' && ch[i + 1] == 'e') {
                ch[i] = ' ';
                ch[i + 1] = ' ';
                e += ch[i];
            } else {
                System.out.println("no ");
                break;
            }
        }
        System.out.println(e);
    }
}

您的问题不是很清楚,但可能是您想要的是(只是修改了您的代码):

for(int i = 0 ; i < ch.length ; i++)
{
    if (ch[i] == 'h' && ch[i+1] == 'e')
    {
       ch[i] = ' ';
       ch[i + 1] = ' ';
       i = i +2;
    }
    else
    {
       e +=ch[i];
    }
}

输入:“我的他是”

输出:“我的”

你可以用这个(我一直在用这个:)):

Str.replace('he', '');

不使用任何内置功能。

import java.util.Scanner;
public class replacechar {
       String line;
       String s = "";
       char from ;
       char to ;
       public replacechar()
       {
           Scanner scan = new Scanner(System.in);
           System.out.println("Enter The String");
           line = scan.nextLine();
           System.out.println("Enter The Character you want to changer");
           from = scan.nextLine().charAt(0);
           System.out.println("Enter the Character you want to replace with");
           to = scan.nextLine().charAt(0);
           replacecharacter(from,to);
       }
       public void replacecharacter(char f,char t)
       {
           for(int i =0;i< line.length();i++)
           {

               if(line.charAt(i) == f)
               {
                  s += t;
               }
               else
               {
                   s += line.charAt(i);
               }

           }
           System.out.println(s);
       }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        replacechar  obj = new replacechar();
    }

}
public static void main(String[] args) {
    String input="my he is";
    String output="";
    String c="he";
    String[] split = input.split(c);
    for(int i=0;i<(split.length);i++)
    {
    output=output.concat(split[i])  ;
    }
    System.out.println(output);
}

split 会正常工作 hr

我们可以将 String 转换为 CharArray 并检查当前字符是否是我们想要删除的字符,如果该字符不是我们想要删除的字符,那么我们将添加到新的字符串中,否则我们不会添加到字符串中

String str = "hello";
String s = "";
char charToRemove = 'h';
char[]ar = str.toCharArray();
for(int i=0;i<ar.length;i++) {
    if(ar[i]!=charToRemove) 
        s+=ar[i];
}   
    System.out.println(s);

暂无
暂无

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

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