首页javastringJava Data Type - 如何搜索特定的字母

Java Data Type - 如何搜索特定的字母

我们想知道如何搜索特定的字母。

public class Main {
  public static void main(String[] args) {
    int y = 0;
    char letter = 'e';
    String food = "this is a test";

    for (int x = 0; x < food.length(); x++) {
      if (food.charAt(x) == letter) {
        y++;
      }
    }

    System.out.println(y);
  }
}