使用 ASCII 值检查字符串是否仅包含Java中的字母
给定一个字符串,现在我们都知道任务是检查一个字符串是否只包含字母。现在我们将逐个字符字符迭代并检查附加到它的相应 ASCII 值。如果未找到,则表示除了“az”或“AZ”之外还有其他字符。如果我们遍历整个字符串并最终找到该域池中字符串中的所有字符,那么该字符串肯定只是字母。
插图:
Input : GeeksforGeeks
Output : True
Input : Geeks4Geeks
Output : False
Input : null
Output : False
在本文中,字符串的字符使用它们的ASCII 值一一检查。
算法:
- 获取字符串
- 匹配字符串:
- 检查字符串是否为空。如果为空,则返回 false
- 检查字符串是否为空。如果为 null,则返回 false。
- 如果字符串既不为空也不为空,则使用 ASCII 值一一检查字符串字符是否为字母表。
- 如果匹配则返回真
示例 1:
Java
// Java Program to Check if a String Contains Only Alphabets
// Using ASCII values
// Importing required classes
import java.util.*;
// Class 1
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating object of helper class
Helper h = new Helper();
// Print statement
System.out.println(
h.isStringOnlyAlphabet("geeksforgeeks"));
}
}
// Class 2
// Helper class
class Helper {
public static boolean isStringOnlyAlphabet(String str)
{
// If string is empty or null
if (str == null || str.equals("")) {
// Return false
return false;
}
// If we reach here we have character/s in string
for (int i = 0; i < str.length(); i++) {
// Getting character at indices
// using charAt() method
char ch = str.charAt(i);
if ((!(ch >= 'A' && ch <= 'Z'))
&& (!(ch >= 'a' && ch <= 'z'))) {
return false;
}
}
// String is only alphabetic
return true;
}
}
Java
// Java program to check if String contains only Alphabets
// Using ASCII Values
// Main class
class GFG {
// Method 1
// To check String for only Alphabets
public static boolean isStringOnlyAlphabet(String str)
{
// If there is no string
if (str == null || str.equals("")) {
return false;
}
// Iterating as now we encounter string
// using length() method
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// Checking for any other aphabetic character
// present in string
if ((!(ch >= 'A' && ch <= 'Z'))
&& (!(ch >= 'a' && ch <= 'z'))) {
// Then string is not only alphabetic
return false;
}
}
// If we reach here, it means
// string is only aphabetic
return true;
}
// Method 2
// Main method
public static void main(String[] args)
{
// Checking for True case
System.out.println("Test Case 1:");
String str1 = "GeeksforGeeks";
System.out.println("Input: " + str1);
System.out.println("Output: "
+ isStringOnlyAlphabet(str1));
// Checking for String with numeric characters
System.out.println("\nTest Case 2:");
String str2 = "Geeks4Geeks";
System.out.println("Input: " + str2);
System.out.println("Output: "
+ isStringOnlyAlphabet(str2));
// Checking for null String
System.out.println("\nTest Case 3:");
String str3 = null;
System.out.println("Input: " + str3);
System.out.println("Output: "
+ isStringOnlyAlphabet(str3));
// Checking for empty String
System.out.println("\nTest Case 4:");
String str4 = "";
System.out.println("Input: " + str4);
System.out.println("Output: "
+ isStringOnlyAlphabet(str4));
}
}
输出
true
示例 2:
Java
// Java program to check if String contains only Alphabets
// Using ASCII Values
// Main class
class GFG {
// Method 1
// To check String for only Alphabets
public static boolean isStringOnlyAlphabet(String str)
{
// If there is no string
if (str == null || str.equals("")) {
return false;
}
// Iterating as now we encounter string
// using length() method
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// Checking for any other aphabetic character
// present in string
if ((!(ch >= 'A' && ch <= 'Z'))
&& (!(ch >= 'a' && ch <= 'z'))) {
// Then string is not only alphabetic
return false;
}
}
// If we reach here, it means
// string is only aphabetic
return true;
}
// Method 2
// Main method
public static void main(String[] args)
{
// Checking for True case
System.out.println("Test Case 1:");
String str1 = "GeeksforGeeks";
System.out.println("Input: " + str1);
System.out.println("Output: "
+ isStringOnlyAlphabet(str1));
// Checking for String with numeric characters
System.out.println("\nTest Case 2:");
String str2 = "Geeks4Geeks";
System.out.println("Input: " + str2);
System.out.println("Output: "
+ isStringOnlyAlphabet(str2));
// Checking for null String
System.out.println("\nTest Case 3:");
String str3 = null;
System.out.println("Input: " + str3);
System.out.println("Output: "
+ isStringOnlyAlphabet(str3));
// Checking for empty String
System.out.println("\nTest Case 4:");
String str4 = "";
System.out.println("Input: " + str4);
System.out.println("Output: "
+ isStringOnlyAlphabet(str4));
}
}
输出
Test Case 1:
Input: GeeksforGeeks
Output: true
Test Case 2:
Input: Geeks4Geeks
Output: false
Test Case 3:
Input: null
Output: false
Test Case 4:
Input:
Output: false
相关文章:
- 使用 Lambda 表达式检查字符串是否仅包含Java中的字母
- 使用 Regex 检查字符串是否仅包含Java中的字母