Java 程序替换字符串中的第一个字符实例

编写一个 Java 程序,用示例替换字符串中的第一个字符实例。在此替换第一个字符实例的示例中,我们将 repstCharStr 字符串中的每个字符与 replace_ch 进行比较。如果它们相等,我们将 new_ch 分配到该索引位置,然后使用 break 语句退出 While 循环。

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);
	}
}

替换字符串中第一个字符实例的输出。

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 程序替换字符串中第一个字符实例示例 2

这个替换第一个字符实例的示例与上面相同,我们将 While 循环 替换为 For 循环

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

replaceFirst”是一个字符串函数,用于将第一个出现的字符替换为新字符。

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

我们可以使用 String 的 substring 函数 来替换第一个字符实例。在此 Java 程序中,repstCharStr.substring(0, i) 返回 replace_ch 字符索引位置之前的子字符串。接下来,我们将其与 Java new_ch 连接。然后,我们使用另一个子字符串 repstCharStr.substring(i + 1, repstCharStr.length() 将字符串从 i 连接到该字符串的末尾。

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