Java Program to Replace First Character Occurrence in a String

Write a Java Program to replace First Character Occurrence in a String with an example. In this Java replace first Occurrence of a Character example, we compare each character in repstCharStr string with the replace_ch. If they are equal, we assigned new_ch in that index position followed by break statement to exit from the While loop.

import java.util.Scanner;

public class ReplaceFirstCharOccur1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String repstCharStr;
		char replace_ch, new_ch;
		int i = 0;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter any String =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter the New Character =  ");
		new_ch = sc.next().charAt(0);
		
		char[] repstCharArr = repstCharStr.toCharArray();
		while(i < repstCharArr.length)
		{
			if(repstCharArr[i] ==  replace_ch) {
				repstCharArr[i] = new_ch;
				break;
			}
			i++;
		}
		System.out.format("\nThe Final String after replacing %c with %c = ", replace_ch, new_ch);
		System.out.print(repstCharArr);
	}
}

Java replace first character occurrence in a String output

Please Enter any String =  Java

Enter the Character to Replace =  a

Enter the New Character =  M

The Final String after replacing a with M = JMva

Java Program to Replace First Character Occurrence in a String Example 2

This Java replace the first occurrence of a character example is the same as the above, and we replaced the While loop with For Loop.

import java.util.Scanner;

public class ReplaceFirstCharOccur2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String repstCharStr;
		char replace_ch, new_ch;
		
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter any String =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter the New Character =  ");
		new_ch = sc.next().charAt(0);
		
		char[] repstCharArr = repstCharStr.toCharArray();
		for(int i = 0; i < repstCharArr.length; i++)
		{
			if(repstCharArr[i] ==  replace_ch) {
				repstCharArr[i] = new_ch;
				break;
			}
		}
		System.out.format("\nThe Final String after replacing %c with %c = ", replace_ch, new_ch);
		System.out.print(repstCharArr);
	}
}
Please Enter any String =  Programming

Enter the Character to Replace =  r

Enter the New Character =  K

The Final String after replacing r with K = PKogramming

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) {
		String repstCharStr;
		String replace_ch, new_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter any String =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Replace =  ");
		replace_ch = sc.nextLine();
		
		System.out.print("\nEnter the New Character =  ");
		new_ch = sc.nextLine();
		
		String x = repstCharStr.replaceFirst(replace_ch, new_ch);
		
		System.out.format("\nThe Final String after replacing %s with %s = %s", 
				replace_ch, new_ch, x);
	}
}
Java Program to Replace First Occurrence of a Character in a String 3

We can use the Java String substring function to replace the first character occurrence. In this Java program, repstCharStr.substring(0, i) returns substring up to the index position of the replace_ch character. Next, we added the new_ch to it. Then, we used another substring repstCharStr.substring(i + 1, repstCharStr.length() to concat string from i to the end of that string.

import java.util.Scanner;

public class ReplaceFirstCharOccur4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String repstCharStr;
		String replace_ch, new_ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter any String =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Replace =  ");
		replace_ch = sc.nextLine();
		
		System.out.print("\nEnter the New Character =  ");
		new_ch = sc.nextLine();
		
		int i = repstCharStr.indexOf(replace_ch);
		String x = repstCharStr.substring(0, i) + new_ch +
					repstCharStr.substring(i + 1, repstCharStr.length() );
	
		System.out.format("\nThe Final String after replacing %s with %s = %s", 
				replace_ch, new_ch, x);
	}
}
Please Enter any String =  Learn Java

Enter the Character to Replace =  a

Enter the New Character =  Q

The Final String after replacing a with Q = LeQrn Java