You can use replaceFirst(String regex, String replacement) method of String.

Answer from Asif on Stack Overflow
🌐
Codecademy
codecademy.com › docs › java › strings › .replacefirst()
Java | Strings | .replaceFirst() | Codecademy
March 15, 2023 - Replaces the first matching substring in a string with the specified replacement string.
🌐
TutorialsPoint
tutorialspoint.com › replace-first-occurrence-of-a-character-in-java
Replace first occurrence of a character in Java
public class Demo { public static void main(String[] args) { String str = "The Haunting of Hill House!"; System.out.println("String: "+str); String res = str.replaceFirst("(?:H)+", "B"); System.out.println("String after replacing a character's first occurrence: "+res); } } String: The Haunting ...
🌐
How to do in Java
howtodoinjava.com › home › string › java string replacefirst()
Java String replaceFirst(): Replace First Occurrence Only
October 11, 2023 - String string = "how to do in java"; String updatedString = string.replaceFirst("\\s", "-"); System.out.println(updatedString); //how-to do in java · Although we can pass the regex as a substring pattern to match, the original string may contain special characters that we would not want to consider in pattern matching. In regex, there are characters that have special meaning.
🌐
Quora
quora.com › How-can-I-replace-first-occurrence-of-a-character-in-a-string-with-another-character-in-Java
How to replace first occurrence of a character in a string with another character in Java - Quora
Answer (1 of 3): We can do this using replace() function in Java. Str.replace(char oldchar, char newchar) : * It replaces the oldchar with newchar in the string Str and returns the modified string. * Example: In the below code, Q in the string "Welcome to Quora" is replaced with q. [code]publ...
🌐
Tutorial Gateway
tutorialgateway.org › home › java programs › java program to replace first character occurrence in a string
Java Program to Replace First Character Occurrence in a String
January 23, 2025 - The replaceFirst is a string function that replaces the first occurrence of a charter with the new character. import java.util.Scanner; public class ReplaceFirstCharOccur3 { private static Scanner sc; public static void main(String[] args) { ...
🌐
Programiz
programiz.com › java-programming › library › string › replacefirst
Java String replaceFirst()
class Main { public static void main(String[] args) { String str1 = "aabbaaac"; String str2 = "Learn223Java55@"; // regex for sequence of digits String regex = "\\d+"; // the first occurrence of "aa" is replaced with "zz" System.out.println(str1.replaceFirst("aa", "zz")); // zzbbaaac // replace the first sequence of digits with a whitespace System.out.println(str2.replaceFirst(regex, " ")); // Learn Java55@ } }
🌐
W3Schools
w3schools.com › java › ref_string_replacefirst.asp
Java String replaceFirst() Method
Tip: See the Java RegEx tutorial to learn about regular expressions. public String replaceFirst(String regex, String replacement) Use a backreference to wrap the first number in parentheses:
Find elsewhere
🌐
Vultr Docs
docs.vultr.com › java › standard-library › java › lang › String › replaceFirst
Java String replaceFirst() - Replace First Occurrence | Vultr Docs
December 20, 2024 - The replaceFirst() function in Java's String class is a versatile method for altering strings by replacing the first occurrence of a pattern defined by a regular expression. Its ability to handle complex replacements using regular expressions ...
🌐
Software Testing Help
softwaretestinghelp.com › home › java › java string replace(), replaceall() & replacefirst() methods
Java String Replace(), ReplaceAll() & ReplaceFirst() Methods
April 1, 2025 - Let’s look into the syntax for more details. ... In this program, we have taken an input String and tried replacing the first occurring character with a new character using the replaceFirst() method.
🌐
TutorialKart
tutorialkart.com › java › replace-first-occurrence-of-a-substring-with-another-in-a-string-using-java
Java - How to Replace First Occurrence of a Substring with Another in String?
May 4, 2023 - Have a Good day."; String old_string = "Bad"; String new_string = "Very-Good"; //replace first occurrence String resultStr = str1.replaceFirst(old_string, new_string); System.out.println(resultStr); } } Run the above program. ... Hi! Very-Good morning. Have a Good day. The the old_string is not present in str1, as a result, replaceFirst() returns str1 unchanged. In this Java Tutorial, we have learned how to replace the first occurrence of a sub-string with another in a string.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › java-string-replace-replacefirst-replaceall-method-examples
Java String replace(), replaceFirst() and replaceAll() methods
September 24, 2015 - The difference between replaceFirst() and replaceAll() method is that the replaceFirst() replaces the first occurrence while replaceAll() replaces all the occurrences. public class JavaExample{ public static void main(String args[]){ String str = new String("Site is BeginnersBook.com"); ...
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_replacefirst.htm
Java - String replaceFirst() Method
The Java String replaceFirst() method is used to replace the first occurrence of a particular pattern in a String object with the given value. This method accepts a regular expression and a replacement string as parameters, searches the current
🌐
Dirask
dirask.com › posts › Java-replace-first-character-in-string-jm3OQD
Java - replace first character in string
Instead of + operator we concatenate ...ubstring(1)); System.out.println(result); // xBC } } In this example, we use string replaceAll() with "^." regex to replace the first character in the text string....
🌐
GitHub
github.com › sourabh48 › Java › blob › master › accenture java › array › String - Find and replace the character (First occurrence) › FirstOccurence.java
Java/accenture java/array/String - Find and replace the character (First occurrence)/FirstOccurence.java at master · sourabh48/Java
import java.util.*; · class FirstOccurence · { public static void main(String args[]) { Scanner scn = new Scanner(System.in); System.out.println("Enter the String"); String input = scn.nextLine(); · int length = input.length(); · System.out.println("Enter the character to be searched "); String userinput = scn.nextLine(); ·
Author: sourabh48
Top answer
1 of 2
1

StringBuilder would be my first idea, as has already been demonstraed, a char array might be an idea, but String already has this functionality built in String#replaceFirst, for example

public static String changeFirst(String in, char old, char with) {
    String oldValue = Pattern.quote(Character.toString(old));
    String withValue = Matcher.quoteReplacement(Character.toString(with));
    return in.replaceFirst(oldValue, withValue);
}

Then you could use it something like...

String replaced = changeFirst("Banana's with Pajamas", 'P', 'K');
System.out.println(replaced);
replaced = changeFirst("Apples", 'P', 'K');
System.out.println(replaced);
replaced = changeFirst("What's with the *uck", '*', 't');
System.out.println(replaced);

Which outputs something like...

Banana's with Kajamas
Apples
What's with the tuck
2 of 2
1

You should show us what you have done unto this point however I am providing a hint here. Note since it is homework I will Not just give you the answer

    public  static String changeFirst(String s, char oldChar, char newChar){
    //now how to implement it 
    //play around with methods like this
    s.indexOf(oldChar);//this gets you the leftmost occurence
    //and 
    //string+char or string+ string or char+char creates a sttring
    //and try "someString".substring(a,b);// creates a substring from a inclusive to //be exclusive (0 is the first       character.) so "foo".substring(0,2).equals("fo") //f is the 0th character o is the first and the second oh is the     2th character //but isnt counted
    //next time put some effort into the questions you ask here let us know all the information and the issues you had
    }
🌐
Javaprogramto
javaprogramto.com › 2020 › 07 › java-string-replacefirst.html
Java String replaceFirst() Example - JavaProgramTo.com
July 21, 2020 - ... In this String API Methods series, You'll learn replaceFirst() method of String class. Replaces the first substring of this string that matches the given regular expression with the given replacement.