Java从字符串中删除单个字符串 [英] Java deletion of single char from string

查看:69
本文介绍了Java从字符串中删除单个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过这个功能,可以多次从字符串中删除一个字符,但它会卡住..。可能有人请帮助



我尝试了什么:



I have tried this function to delete a single char from string many times but it stucks .. .can anyone please help

What I have tried:

public String deleteChar (String s1, char c1)
{
    int i,j;
    char s2[]=s1.toCharArray();
    for(i=0;i<s2.length;i++)
    {
        if(s2[i]==c1)
        {
            j=i;

            while(j<s2.length-1)
            {
                s2[j]=s2[j+1] ;
            }
        s2[j+1]=' ' ;
        }
    }
    return String.valueOf(s2);
}





和这个主要方法





and this main method

public static void main(String args[])     {       
    System.out.println("Enter the input String:");         
    Scanner scan = new Scanner(System.in);         
    Str str2 = new Str(scan.nextLine());         
    System.out.println("Enter the character to be deleted :");         
    char c1 = scan.next().charAt(0) ;         
    str2.input = str2.deleteChar(str2.input, c1);         
    System.out.println("The output String is :" + str2.input);
}

推荐答案

它甚至可以编译吗?在行中

Does it even compile? In the line
Str str2 = new Str(scan.nextLine());

Str 不是有效的类型。



此外,在您移动文本的而块中,忘了增加索引变量的值 j

Str is not a valid type.

Also, in your while block where you are moving the text, you forgot to increment the value of your index variable j.


引用:

它卡住



定义卡住的类型。

假设'卡住'意味着你没有得到预期结果,最好是在调试器中运行代码并查看它的作用。




Define the kind of "stuck".
Assuming that 'stuck' means that you don't get expected result, the best is to run the code in debugger and see what it does.

str2.input = str2.deleteChar(str2.input, c1);



据我所知,'deleteChar'不是字符串方法。



当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看你的代码是什么这样做。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java -application.html [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


As far as I understand, 'deleteChar' is not a string method.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于Java从字符串中删除单个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆