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

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

接下来,我们将该字符与用户给定的 ch 字符进行比较,以检查它们是否相等。如果为真,我们将标志值增加 1,然后 break 语句将退出循环。接下来,我们使用 Java If Else 语句检查标志值是否等于 0。如果为假,则打印字符索引位置。

import java.util.Scanner;

public class FirstCharOccurence1 {
	private static Scanner sc;
	public static void main(String[] args) {
		String firstCharStr;
		char ch;
		int i = 0, flag = 0;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String to Find First Char Occur =  ");
		firstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Find =  ");
		ch = sc.next().charAt(0);
		
		while(i < firstCharStr.length())
		{
			if(firstCharStr.charAt(i) ==  ch) {
				flag++;
				break;
			}
			i++;
		}
		if(flag == 0) {
			System.out.println("\nSorry! We haven't found the Character ");
		}
		else {
			System.out.format("\nThe First Character Occurrence of %c at %d position", 
					ch, i);
		}
	}

}

Java 使用 while 循环查找字符串中的第一个字符出现位置的输出

Enter String to Find First Char Occur =  hello

Enter the Character to Find =  l

The First Character Occurrence of l at 2 position

Java 程序使用 For 循环查找字符串中的第一个字符出现位置

import java.util.Scanner;

public class FirstCharOccurence2 {
	private static Scanner sc;
	public static void main(String[] args) {
		String firstCharStr;
		char ch;
		int i, flag = 0;
		
		sc= new Scanner(System.in);

		System.out.print("\nEnter String to Find First Char Occur =  ");
		firstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Find =  ");
		ch = sc.next().charAt(0);
		
		for(i = 0; i < firstCharStr.length(); i++)
		{
			if(firstCharStr.charAt(i) ==  ch) {
				flag++;
				break;
			}
		}
		if(flag == 0) {
			System.out.println("\nSorry! We haven't found the Character ");
		}
		else {
			System.out.format("\nThe First Character Occurrence of %c at %d position", 
					ch, i);
		}
	}

}
Enter String to Find First Char Occur =  java

Enter the Character to Find =  a

The First Character Occurrence of a at 1 position

在 Java 编程中,有一个名为 indexOf字符串函数。此 indexOf 函数返回字符串中第一个字符出现位置的索引。接下来,我们使用字符串 charAt 函数在那个索引位置返回字符。

import java.util.Scanner;

public class FirstCharOccurence3 {
	private static Scanner sc;
	public static void main(String[] args) {
		String firstCharStr;
		char ch;
		
		sc= new Scanner(System.in);

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

Java StringBuilder 和 StringBuffer 也具有 indexOf 和 charAt 函数来返回字符串的第一个字符出现位置。在此 Java 示例中,我们转换了给定的字符串到 StringBuilder、StringBuffer,并返回输出。

import java.util.Scanner;

public class FirstCharOccurence4 {
	private static Scanner sc;
	public static void main(String[] args) {
		String firstCharStr;
		char ch;
		
		sc= new Scanner(System.in);

		System.out.print("\nPlease Enter String to find First Occurrence =  ");
		firstCharStr = sc.nextLine();
		
		System.out.print("\nEnter the Character to Find =  ");
		ch = sc.next().charAt(0);
		
		StringBuilder sb = new StringBuilder(firstCharStr);
		int x = sb.indexOf(String.valueOf(ch));
		
		System.out.format("\nThe First Character Occurrence of %c at %d position", 
				sb.charAt(x), x);
		
		StringBuffer sbuff = new StringBuffer(firstCharStr);
		int y = sb.indexOf(String.valueOf(ch));
		
		System.out.format("\nThe First Character Occurrence of %c at %d position", 
				sbuff.charAt(y), y);
	}
}
Please Enter String to find First Occurrence =  tutorial gateway

Enter the Character to Find =  a

The First Character Occurrence of a at 6 position
The First Character Occurrence of a at 6 position

请参考以下字符串程序。

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