Java 程序查找字符串中最后一个字符的出现位置

编写一个Java程序,通过示例查找字符串中最后一个字符的出现位置。在这个最后一个字符出现位置的示例中,我们使用While循环从头到尾迭代lastCharStr。我们在while循环中使用String charAt函数来获取lastCharStr中每个索引位置的字符。 

接下来,我们将该字符与ls​​ch字符进行比较,以检查它们是否相等。如果为True,我们将i值赋给索引变量并递增i值。接下来,我们使用Java If Else语句来检查索引值是否等于-1(未找到)。如果为False,则打印字符索引位置。

import java.util.Scanner;

public class LastCharOccurence1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lastCharStr;
		int i = 0, lIndex = -1;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to find Last Occurrence =  ");
		lastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Last Character to Find =  ");
		int lsch = sc.next().charAt(0);
		
		while(i < lastCharStr.length())
		{
			if(lastCharStr.charAt(i) ==  lsch) {
				lIndex = i;
			}
			i++;
		}
		if(lIndex == -1) {
			System.out.println("\nSorry! We haven't found the Character ");
		}
		else {
			System.out.format("\nThe Last Character Occurrence of %c at %d position", lsch, lIndex);
		}
	}

}

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

Please Enter String to find Last Occurrence =  java programmings

Enter the Last Character to Find =  a

The Last Character Occurrence of a at 10 position

Java 程序查找字符串中最后一个字符的出现位置 示例 2

这与上面的Java最后一个字符出现位置示例相同,我们将While循环替换为For循环

import java.util.Scanner;

public class LastCharOccurence2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lastCharStr;
		int i, lIndex = 0;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String for Last Occurrence =  ");
		lastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Last Character to Find =  ");
		char lsch = sc.next().charAt(0);
		
		for(i = 0; i < lastCharStr.length(); i++)
		{
			if(lastCharStr.charAt(i) ==  lsch) {
				lIndex = i;
			}
		}
		if(lIndex == -1) {
			System.out.println("\nSorry! We haven't found the Character ");
		}
		else {
			System.out.format("\nThe Last Character Occurrence of %c at %d position", lsch, lIndex );
		}
	}

}
Please Enter String to find Last Occurrence =  learn java

Enter the Last Character to Find =  a

The Last Character Occurrence of a at 9 position

在Java编程中,有一个名为lastIndexOf的字符串函数。此lastIndexOf函数返回ls​​ch字符在字符串中的最后一个索引位置。接下来,我们使用lastCharStr上的字符串charAt函数来返回该索引位置的字符。

import java.util.Scanner;

public class LastCharOccurence3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lastCharStr;
		char lsch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to find Last Occurrence =  ");
		lastCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Last Character to Find =  ");
		lsch = sc.next().charAt(0);
		
		int x = lastCharStr.lastIndexOf(lsch);
		
		System.out.format("\nThe Last Character Occurrence of %c at %d position", 
				lastCharStr.charAt(x), x);
	}
}
Java Program to Find Last Character Occurrence in a String

Java StringBuilder和StringBuffer也有lastIndexOf和charAt函数来返回字符串的最后一个字符的出现位置。在此Java示例代码中,我们将给定的字符串转换为StringBuilder和StringBuffer,并返回输出。

import java.util.Scanner;
public class LastCharOccurence4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String lastCharStr;
		char ch;		
		sc= new Scanner(System.in);
		System.out.print("\nPlease Enter String to find Last Occurrence =  ");
		lastCharStr = sc.nextLine();		
		System.out.print("\nEnter the Character to Find =  ");
		ch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(lastCharStr);
		int a = sb.lastIndexOf(String.valueOf(ch));
		
		System.out.format("\nThe Last Character Occurrence of %c at %d position", 
				sb.charAt(a), a);
		
		StringBuffer sbuff = new StringBuffer(lastCharStr);
		int b = sb.lastIndexOf(String.valueOf(ch));
		
		System.out.format("\nThe Last Character Occurrence of %c at %d position", 
				sbuff.charAt(b), b);
	}
}
lease Enter String to find Last Occurrence =  hello world l

Enter the Character to Find =  l

The Last Character Occurrence of l at 12 position
The Last Character Occurrence of l at 12 position