我想不出在 stringbuilder 中找到特定字符并将其替换为大写字母的方法

I can't figure out a way to find a specific char in a stringbuilder and replace it with uppercase

例如: 输入一个字符串: aAaAaAaAaAaAbBbBbBbBbBbBbcCccccCCccCCCC

什么字符应该转换为大写? b

输出应该是什么: aAaAaAaAaAaABBBBBBBBBBBBBcCccccCCccCCCC

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a String");
    StringBuilder sb = new StringBuilder(keyboard.nextLine());
    System.out.println("Which character should be converted to uppercase?");
    char c = keyboard.nextLine().charAt(0);

    ParsingUtils.changeLetter(sb, c);

    System.out.println(sb);
}

public static void changeLetter(StringBuilder sb, char letter)
{
    String change = Character.toString(letter); //changes the char in the parameter to string
    String change2 = change.toUpperCase(); //make an uppercase copy of the string
    char upper = change2.charAt(0); //change the upper case copy to a char

    for (int counter = 0; counter < sb.length(); counter++) //Is supposed to replace every instance of the given character with uppercase
    {
        if (sb.toString().contains(change))
        {
            sb.setCharAt(counter, upper);
        }
    }
}

我的代码输出:BBBBBBBBBBBBBBBBBBBBBBBBcCccccCCccCCCC

有没有更好的、更简单的方法来做到这一点,我很沮丧,看不到?

我在 Java 方面还是个新手,所以可能有一个简单的答案完全超出了我的理解范围。

  • 前三行可以简化为:

    char upper = Character.toUpperCase(letter);
    
  • 使用 charAt(counter),而不是检查整个字符串生成器是否包含该字母。

    if (sb.charAt(counter) == letter)
    

完整代码:

public static void changeLetter(StringBuilder sb, char letter)
{
    char upper = Character.toUpperCase(letter);

    for (int counter = 0; counter < sb.length(); counter++)
    {
        if (sb.charAt(counter) == letter)
        {
            sb.setCharAt(counter, upper);
        }
    }
}