简体   繁体   English

Java 删除字符串的最后一个字符

[英]Deleting Last Character From a String in Java

I'm writing a java code which gives an error when typing more than 10 characters in a PasswordField and cannot type from there on.我正在编写一个 java 代码,当在 PasswordField 中输入超过 10 个字符时会出错,并且无法从那里输入。 I tried deleting the last character from the PasswordField on a KeyPressed event but instead of deleting the last character, it deletes the character before it and replace it with the last character.Here goes my code.我尝试从 KeyPressed 事件的 PasswordField 中删除最后一个字符,但不是删除最后一个字符,而是删除它之前的字符并将其替换为最后一个字符。这是我的代码。

private void passFieldKeyTyped(java.awt.event.KeyEvent evt) {私有无效passFieldKeyTyped(java.awt.event.KeyEvent evt){

String pw1 = new String(passField.getPassword());
    
    if(passField.getPassword().length==10){

        try{

          StringBuffer bf = new StringBuffer(pw1);
           bf.deleteCharAt(10);
          String pw2 = new String(bf);    
           passField.setText(pw2);
                             
          JOptionPane.showMessageDialog(this, "<html><h4>Password Must Not Contain More Than 10 Characters !</h4></html>", "Error !", JOptionPane.ERROR_MESSAGE);

           }

        catch(Exception e){
            
           JOptionPane.showMessageDialog(this, e.getMessage());
        }

    }

I'm still a newbie for programming.我仍然是编程的新手。 I hope someone can help me with this.我希望有人能帮我解决这个问题。 Thanks !谢谢 !

public class RemoveChar {  
    public static void main(String[] args) {  
              String str = "India is my country";  
              System.out.println(charRemoveAt(str,));  
           }  
           public static String charRemoveAt(String str) {  
              return str.substring(0, str.length()-1);  
           }  
}  

I guess you are trying to delete a char from the String.我猜你正试图从字符串中删除一个字符。

You can use the StringBuilder class here to delete a character at your specified position.您可以在此处使用StringBuilder类删除指定位置的字符。 First convert your String to StringBuilder -首先将您的字符串转换为StringBuilder -

StringBuilder sb = new StringBuilder(inputString);

Then you can use the built in method deleteCharAt() to delete the char at your desired position like this -然后您可以使用内置方法deleteCharAt()删除您所需位置的字符,如下所示 -

sb.deleteCharAt(10);

Hope this may help you.希望这可以帮助你。

Guys I'm so sorry I found out it was a mistake of mine..I actually had to check the length of the passwordfield again and length must be 11 to delete the 10th character.伙计们,我很抱歉我发现这是我的错误。实际上我不得不再次检查密码字段的长度,长度必须是 11 才能删除第 10 个字符。 Here goes the code.It worked..Thanks for helping guys !!!!!!!!这是代码。它有效..感谢您的帮助!!!!!!!!!

private void passFieldKeyTyped(java.awt.event.KeyEvent evt) {私有无效passFieldKeyTyped(java.awt.event.KeyEvent evt){

String pw1 = new String(passField.getPassword());
    
    if(passField.getPassword().length==11){

        try{

          StringBuffer bf = new StringBuffer(pw1);
           bf.deleteCharAt(10);
          String pw2 = new String(bf);    
           passField.setText(pw2);
                             
          JOptionPane.showMessageDialog(this, "<html><h4>Password Must Not Contain More Than 10 Characters !</h4></html>", "Error !", JOptionPane.ERROR_MESSAGE);

           }

        catch(Exception e){
            
           JOptionPane.showMessageDialog(this, e.getMessage());
        }

    }

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

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