我正在尝试从字符串中删除字符

发布于 2025-02-14 01:26:34 字数 781 浏览 2 评论 0原文

我正在进行测试,但是在测试后我会收到NULL,并且正在接收一个ostertionfailedError

    assertEquals("", this.myCustomString.remove(""));
    
    this.myCustomString.setString(null);
    assertEquals("", this.myCustomString.remove(""));
    
    this.myCustomString.setString("my lucky numbers are 6, 8, and 19.");
    assertEquals("my lucky numbes e 6, 8, nd 19.", this.myCustomString.remove("ra6"));

public String remove(String arg){//从字符串中删除指定的字符

       if (myString == null || myString == "") {

           return this.myString;

           }

           if (myString != null) {

           this.myString = myString.replaceAll(arg,"");

           return myString;

           }
          
           return myString;
        
           
   }

i am running a test but i am receiving null after the test and i am receiving a AssertionFailedError

    assertEquals("", this.myCustomString.remove(""));
    
    this.myCustomString.setString(null);
    assertEquals("", this.myCustomString.remove(""));
    
    this.myCustomString.setString("my lucky numbers are 6, 8, and 19.");
    assertEquals("my lucky numbes e 6, 8, nd 19.", this.myCustomString.remove("ra6"));

public String remove(String arg){//removes specified characters from the string

       if (myString == null || myString == "") {

           return this.myString;

           }

           if (myString != null) {

           this.myString = myString.replaceAll(arg,"");

           return myString;

           }
          
           return myString;
        
           
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2025-02-21 01:26:34

这应该有帮助

    String result = "";

       if (myString == null || myString == "") {
           return ""; 
       }
       //places the String arg into a character array
         char[] c = arg.toCharArray();
  // then use a for loop to check if there is only letters in the string arg
  // after we have checked to see if there is only letters when remove anything that is not letters
       for(int i= 0; i< c.length; i++){
             if (Character.isLetter(c[i])) {
                        result = result + c[i];
                    }
                  }
 // when then use the result from above and place it into its own character array, we then use the characters
 // from the array to be replaced by blank

         for (char ch : result.toCharArray()) {
                 myString = myString.replace(String.valueOf(ch), "");
            }
                  
            
                  return myString;
                 
                }
            
           

This should help

    String result = "";

       if (myString == null || myString == "") {
           return ""; 
       }
       //places the String arg into a character array
         char[] c = arg.toCharArray();
  // then use a for loop to check if there is only letters in the string arg
  // after we have checked to see if there is only letters when remove anything that is not letters
       for(int i= 0; i< c.length; i++){
             if (Character.isLetter(c[i])) {
                        result = result + c[i];
                    }
                  }
 // when then use the result from above and place it into its own character array, we then use the characters
 // from the array to be replaced by blank

         for (char ch : result.toCharArray()) {
                 myString = myString.replace(String.valueOf(ch), "");
            }
                  
            
                  return myString;
                 
                }
            
           
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文