【问题标题】:Stop at the first occurence of a character在字符第一次出现时停止
【发布时间】:2017-10-13 23:28:39
【问题描述】:

我只想删除“ brealdeke ”中的“a” 这个程序有效,它打印“breldeke”,但如果我把 "brealdeake" 在这个字符串中有 2 个 'a',它会发疯并打印:breldeakebrealdeke 如何修复它?谢谢 我真的希望它看起来像这样:

class Example {
    public static String suppression(char c, String s) {
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                int position = i;
                for (int a = 0; a < position; a++) {
                    System.out.print(s.charAt(a));
                }
                for (int b = position + 1; b < s.length(); b++) {
                    System.out.print(s.charAt(b));
                }
            }

        }
        return "";
    }

    public static void main(String[] args) {
        // prints "breldeke"
        System.out.println(suppression('a', "brealdeke"));
        // prints "breldeakebrealdeke"
        System.out.print(suppression('a', "brealdeake"));
    }
}

【问题讨论】:

  • 所以要删除第一个重复出现的第一个字符?
  • 嗯不是“重复”,我不知道如何解释,我是选择必须删除的字母的人这里是“软件”中的几个例子'a' - -> 在 "Software" 中打印 Softwre 或 'o' --> 在 "Tomato" 中打印 Sftware 或 'o' --> 打印 Tmato

标签: java charat


【解决方案1】:

你可以试试:

"banana".replaceFirst("a", "");

这将返回bnana

编辑:希望这不包括您尚未学过的任何内容

public static void main(String[] args) {
    String word = "banana";
    String strippedWord = "";
    boolean found = false;
    for (int i = 0; i < word.length(); i++) {
        if (word.charAt(i) == 'a' && !found) found = !found;
        else strippedWord += word.charAt(i);
    }
    System.out.println(strippedWord);
}

这打印bnana

EDIT2 :你说你想要它在一个函数中,同样适用:

public static String suppression(char c, String word) {
    String strippedWord = "";
    boolean charRemoved = false;    // This is a boolean variable used to know when the char was skipped!
    for (int i = 0; i < word.length(); i++) {
        // If the current letter is for example 'a' and we haven't yet skipped the char, skip this char we're at 
        if (word.charAt(i) == c && charRemoved == false) charRemoved = true;
        else strippedWord += word.charAt(i);
    }
    return strippedWord;
}

public static void main(String[] args) {
    // prints "breldeke"
    System.out.println(suppression('a', "brealdeke"));
    // prints "breldeake"
    System.out.print(suppression('a', "brealdeake"));
}

【讨论】:

  • 我总是忘记 Java 有所有这些几乎不切实际的操作。
  • 您好,谢谢您的回答,但由于我们还没有在学校学习过“.replace”,他们不允许我们使用它:s 到目前为止,我们知道:charAt(), .length() , stringEquals
  • 谢谢你,我真的很感激你为我所做的一切,非常感谢,但也从未见过:s 我已经编辑了我的帖子,如果你能看一下,试着让它不那么混乱再次感谢
  • found 只是一个布尔变量,这里我已更改代码以将其放入您的函数中!
【解决方案2】:

附加变量好吗?

我个人会这样做:

public static String removeFirstLetter(string s, char c)
{
   String word = "";
   Bool foundChar = false;

   for ( int i = 0; i<s.length();i++) {
      if (s.charAt(i).toLower() != c)
      {
         word += s.char(i);
      }
    else
    {
       if (foundChar == false){
           foundChar = true;
       }  
       else
       {
           word += s.char(i);
       }
     }
   }
}

 System.out.print(word);

【讨论】:

  • 您好,也感谢您的回答,但由于我们还没有在学校学习过“toLower”,所以他们不允许我们使用它:s 到目前为止,我们知道:charAt() , .length() , stringEquals
  • @Blebhebhe 如果不关心情况,您可以忽略它
  • 感谢感谢,我们确实知道“布尔”,但我的作业是关于使用函数,请你再看看我的帖子,有人对其进行了编辑以使其看起来不那么凌乱
  • 所有你需要做的就是将上面的代码包装在一个方法/函数中来调用。我会更新的。
猜你喜欢
  • 2012-07-03
  • 1970-01-01
  • 2020-05-04
  • 2014-12-04
  • 2021-01-26
  • 1970-01-01
  • 2022-12-05
  • 2016-09-28
  • 1970-01-01
相关资源
最近更新 更多