简体   繁体   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

Can anyone tell me how to remove character from string without any use of built-in functions and stringbuffer or builder.谁能告诉我如何在不使用任何内置函数和 stringbuffer 或构建器的情况下从字符串中删除字符。 Although I tried to form a logic but didn't get the answer.虽然我试图形成一个逻辑但没有得到答案。 As in below code I want to remove 'he' from the inputstring.在下面的代码中,我想从输入字符串中删除“他”。

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

Your question is not very clear, but may be what you want is(just modified your code) :您的问题不是很清楚,但可能是您想要的是(只是修改了您的代码):

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

Input : "My he is"输入:“我的他是”

Output : "My is"输出:“我的”

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

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

With not using any build in function.不使用任何内置功能。

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 will work fine hr split 会正常工作 hr

we can convert a String toCharArray and check current character is which we want to remove or not if the character is not which we want to remove then we will add to the new String else we wont add to the String我们可以将 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.

相关问题 Java程序无需使用任何内置方法即可读取字符串和字符并显示给定字符的最后一次出现的索引 - java program to read a String and a character and display the index of the last occurence of the given character without using any built in method 如何删除java中字符串缓冲区的最后一个字符? - How to remove last character of string buffer in java? 如何不使用StringBuilder从字符串(而不是数组)中删除重复项? - How to remove duplicates from string (not array) without using StringBuilder? Java中的String中的StringBuilder - StringBuilder from String in Java 在没有StringBuilder的Java中反转字符串 - Reverse a string in Java without StringBuilder 如何正确使用 Map 的方法 merge()<string, stringbuilder> 到 append 一个特定的字符?</string,> - How to correctly use the method merge() of Map <String, StringBuilder> to append a specific character? 在 Java 中使用 StringBuilder 与 String - Use of StringBuilder vs String in Java 如何在Java中没有Stringbuilder的情况下增加一个字符串的长度以仅与另一个字符串匹配? - How do i increase the length of a String to match another String only once WITHOUT Stringbuilder in java? 在Java中从字符串中删除一个字符 - Remove a Character From String in Java 如何从字符串中删除字符? - How do I remove a character from a String?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM