Java 程序:移除字符串中字符的最后一次出现

编写一个 Java 程序,以示例移除或删除字符串中字符的最后一次出现。在此 Java 移除字符的最后一次出现示例中,我们使用了 StringBuilder 的 lastIndexOf 和 deleteCharAt 函数。

要获取 delLastCharStr 的最后索引位置,我们使用了 lastIndexOf,delLastCharStr.lastIndexOf(del_lch) 返回最后索引位置。接下来,我们使用 sb.deleteCharAt(i) 来删除或移除 sb 中的该字符。此处,我们也可以使用 i = sb.lastIndexOf(del_lch);

import java.util.Scanner;

public class DeleteLastCharOcc1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delLastCharStr;
		char del_lch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Delete Last Character =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_lch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(delLastCharStr);
		int i = delLastCharStr.lastIndexOf(del_lch);
		sb.deleteCharAt(i);
		
		System.out.println("\nThe Final String after Deleting Last Occurrence of " + 
				del_lch + " Character = " + sb);
	}
}

Java 程序:移除字符串中字符的最后一次出现 的输出

Please Enter String to Delete Last Character =  Java

Enter the Character to Delete =  a

The Final String after Deleting Last Occurrence of a Character = Jav

Java StringBuffer 也具有 lastIndexOf 和 deleteCharAt 函数,用于移除字符串中字符的最后一次出现。

import java.util.Scanner;

public class DeleteLastCharOcc2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delLastCharStr;
		char del_lch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Delete Last Character =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_lch = sc.next().charAt(0);
		
		StringBuffer sbuff = new StringBuffer(delLastCharStr);
		int i = delLastCharStr.lastIndexOf(del_lch);
		sbuff.deleteCharAt(i);
		
		System.out.println("\nThe Final String after Deleting Last Occurrence of " + 
		del_lch + " = " + sbuff);
	}
}
Please Enter String to Delete Last Character =  tutorial

Enter the Character to Delete =  t

The Final String after Deleting Last Occurrence of t = tuorial

Java 程序:移除字符串中字符的最后一次出现 示例 3

在此 Java 移除字符的最后一次出现示例中,我们使用 For 循环从后往前遍历 delLastCharStr。在循环内,我们比较 delLastCharStr 的字符与 del_lch。如果它们相等,break 语句将退出 for 循环。接下来,我们使用 StringBuilder 和 StringBuffer 的 deleteCharAt 函数来移除最后一个字符。

import java.util.Scanner;

public class DeleteLastCharOcc3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delLastCharStr;
		char del_lch;
		int i;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Delete Last Character =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_lch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(delLastCharStr);
		StringBuffer sbuff = new StringBuffer(delLastCharStr);
		
		for(i = delLastCharStr.length() - 1; i >= 0; i--) {
			if(delLastCharStr.charAt(i) == del_lch) {
				break;
			}
		}		
		System.out.format("\nThe Last Character Occurence of %c at %d position", del_lch, i);
		
		sb.deleteCharAt(i);		
		System.out.println("\n\nThe Final String after Deleting Last Occurrence of " + 
				del_lch + " Character = " + sb);
		
		sbuff.deleteCharAt(i);	
		System.out.println("The Final String after Deleting Last Occurrence of " 
		+ del_lch + " = " + sbuff);
	}
}
Please Enter String to Delete Last Character =  programming language

Enter the Character to Delete =  g

The Last Character Occurence of g at 18 position

The Final String after Deleting Last Occurrence of g Character = programming languae
The Final String after Deleting Last Occurrence of g = programming languae

要移除字符的最后一次出现,我们还可以使用 Java String 的 substring 函数。在此 Java 程序中,delLastCharStr.lastIndexOf(del_lch) 查找字符最后一次出现的索引位置。接下来,delLastCharStr.substring(0, i) + delLastCharStr.substring(i + 1) 返回不包含索引位置 i 处字符的字符串。

import java.util.Scanner;

public class DeleteLastCharOcc4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delLastCharStr;
		char del_lch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Delete Last Character =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_lch = sc.next().charAt(0);
		
		int i = delLastCharStr.lastIndexOf(del_lch);
		String out = delLastCharStr.substring(0, i) + delLastCharStr.substring(i + 1);
		
		System.out.println("\nThe Final String after Deleting Last Occurrence of " + 
				del_lch + " Character = " + out);
	}
}
Java Program to Remove Last occurrence of a Character in a String

在此 Java 移除字符的最后一次出现示例中,我们使用 toCharArray() 将 delLastCharStr 字符串转换为 delLastCharArr 字符数组。接下来,在 for 循环内,我们比较 delLastCharStr 字符串的字符与 del_lch。如果它们相等,我们将 delLastCharArr 的该索引位置赋值为 0 或空字符,然后使用 Java break 语句 退出 for 循环

import java.util.Scanner;

public class DeleteLastCharOcc5 {
	private static Scanner sc;
	public static void main(String[] args) {
		String delLastCharStr;
		char del_lch;
		int i, len;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to Delete Last Character =  ");
		delLastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Delete =  ");
		del_lch = sc.next().charAt(0);
		
		char[] delLastCharArr = delLastCharStr.toCharArray();
		len = delLastCharArr.length;
		
		for(i = len - 1; i >= 0; i--) {			
			if(delLastCharArr[i] == del_lch) {
				delLastCharArr[i] = 0;
				break;
			}
		}

		System.out.print("\nThe Final String after Deleting Last Occurrence of " + 
				del_lch + " Character = " );
		System.out.print(delLastCharArr);
		
	}
}
Please Enter String to Delete Last Character =  hello world

Enter the Character to Delete =  l

The Final String after Deleting Last Occurrence of l Character = hello word

请参考以下字符串程序。

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