Java 程序替换字符串中最后一个字符的出现

编写一个 Java 程序来替换字符串中最后一个字符的出现,并附带示例。在此 Java 替换字符串最后一个字符出现示例中,我们使用 toCharArray() 将 repstCharStr 转换为 replastCharArr 字符数组。 

接下来,我们使用 While 循环从后往前(反向)迭代 repstCharStr 字符串的字符。在循环中,我们将 replastCharArr 数组中的每个字符与 replace_ch 进行比较。如果它们相等,我们将 new_ch 赋给该 replastCharArr 索引位置,然后使用 break 语句退出 While 循环。

import java.util.Scanner;

public class ReplaceLastCharOccur1 {
	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("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		char[] replastCharArr = repstCharStr.toCharArray();
		int i = replastCharArr.length - 1;
		while(i >= 0)
		{
			if(replastCharArr[i] ==  replace_ch) {
				replastCharArr[i] = new_ch;
				break;
			}
			i--;
		}
		System.out.format("\nAfter replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(replastCharArr);
	}
}

Java 替换字符串中最后一个字符出现的输出

Enter a String to replace last Char Occurrence =  java

Enter a Character to Replace =  a

Enter a New Character =  T

After replacing Last Occurrence of a with T = javT

此 Java 替换字符串中最后一个字符出现的示例与上面相同,我们用 For 循环 替换了 While 循环

import java.util.Scanner;

public class ReplaceLastCharOccur2 {
	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("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		char[] replastCharArr = repstCharStr.toCharArray();
		
		for(int i = replastCharArr.length - 1; i >= 0; i--)
		{
			if(replastCharArr[i] ==  replace_ch) {
				replastCharArr[i] = new_ch;
				break;
			}
		}
		System.out.format("\nAfter replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(replastCharArr);
	}
}
Enter a String to replace last Char Occurrence =  tutorial

Enter a Character to Replace =  t

Enter a New Character =  M

After replacing Last Occurrence of t with M = tuMorial

Java 程序示例 3:替换字符串中最后一个字符的出现

在 Java 编程中,我们使用 lastIndexOf 来获取 replace_ch 在 repstCharArr 中的最后一个索引位置。然后,我们在该位置赋给 new_ch。

import java.util.Scanner;

public class ReplaceLastCharOccur3 {
	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("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		char[] repstCharArr = repstCharStr.toCharArray();;
		int i = repstCharStr.lastIndexOf(replace_ch);
		repstCharArr[i] = new_ch;

		System.out.format("\nAfter replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(repstCharArr);
	}
}
Java Program to Replace Last Character Occurrence in a String

我们可以使用 Java String 的 substring 函数 来替换最后一个字符的出现。在此 Java 程序中,repstCharStr.lastIndexOf(replace_ch) 查找最后一个索引位置。接下来,repstCharStr.substring(0, i) 返回 i 之前的子字符串。然后,我们在其后添加 new_ch。之后,我们使用另一个子字符串 repstCharStr.substring(i + 1, repstCharStr.length()) 来连接从 i 到字符串末尾的部分。

import java.util.Scanner;

public class ReplaceLastCharOccur4 {
	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("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		int i = repstCharStr.lastIndexOf(replace_ch);
		String out = repstCharStr.substring(0, i) + new_ch + repstCharStr.substring(i + 1, repstCharStr.length() );

		System.out.format("\nAfter replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(out);
	}
}
Enter a String to replace last Char Occurrence =  tutorial gateway

Enter a Character to Replace =  t

Enter a New Character =  X

After replacing Last Occurrence of t with X = tutorial gaXeway

Java 程序使用 StringBuilder 替换字符串中最后一个字符的出现

Java StringBuilder 也有 sb.lastIndexOf(String.valueOf(replace_ch)),但它接受字符串值,因此我们将 replace_ch 转换为字符串。然后,deleteCharAt 函数删除该字符。接着,sb.insert(j, new_ch) 函数在该位置插入 new_ch 字符。

import java.util.Scanner;

public class ReplaceLastCharOccur5 {
	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("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(repstCharStr);
		//int i = repstCharStr.lastIndexOf(replace_ch);
		int j = sb.lastIndexOf(String.valueOf(replace_ch));
		sb.deleteCharAt(j);
		sb.insert(j, new_ch);

		System.out.format("\nAfter "
				+ "replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(sb);
	}
}
Enter a String to replace last Char Occurrence =  java programming

Enter a Character to Replace =  a

Enter a New Character =  L

After replacing Last Occurrence of a with L = java progrLmming

Java StringBuffer 也有 lastIndexOf、deleteCharAt、insert 函数。因此,我们使用这些函数来替换字符串中最后一个字符的出现。

import java.util.Scanner;

public class ReplaceLastCharOccur6 {
	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("\nEnter a String to replace last Char Occurrence =  ");
		repstCharStr = sc.nextLine();
		
		System.out.print("\nEnter a Character to Replace =  ");
		replace_ch = sc.next().charAt(0);
		
		System.out.print("\nEnter a New Character =  ");
		new_ch = sc.next().charAt(0);
		
		StringBuffer sbuff = new StringBuffer(repstCharStr);
		//int i = repstCharStr.lastIndexOf(replace_ch);
		
		int j = sbuff.lastIndexOf(String.valueOf(replace_ch));
		sbuff.deleteCharAt(j);
		sbuff.insert(j, new_ch);

		System.out.format("\nAfter "
				+ "replacing Last Occurrence of %c with %c = ", replace_ch, new_ch);
		System.out.print(sbuff);
	}
}
Enter a String to replace last Char Occurrence =  hello everyone

Enter a Character to Replace =  e

Enter a New Character =  W

After replacing Last Occurrence of e with W = hello everyonW

请参考以下字符串程序。

  1. 计算字符串中字符的总出现次数
  2. 查找字符串中所有字符的出现次数
  3. 字符串中第一个字符的出现次数
  4. 替换字符串中某个字符的首次出现
  5. 删除字符串中某个字符的首次出现
  6. 字符串中最后一个字符的出现次数
  7. 删除字符串中最后一个字符的出现次数
  8. 字符串中的第一个和最后一个字符
  9. 删除字符串中的第一个和最后一个字符
  10. 字符串中出现次数最多的字符
  11. 字符串中出现次数最少的字符
  12. 字符串中每个字符的频率
  13. 删除字符串中某个字符的所有出现