方法一:
从字符串中分离出字符,依次将每一个字符与'a','z'比较,判断是否为小写字母,与'A','Z'比较,判断是否为大写字母,其他的为非英文字母。
方法二:
从字符串中分离出字符,依次将每一个字符在'a'~'z'中查找,如果查到则为小写字母,同样方法判断是否为大写字母,其他的为非英文字母。
方法三:
从字符串中分离出字符,依次将每一个字符用Character.isLowerCase()方法判断是否为小写字母,Character.isUpperCase()判断是否为大写字母,其他的为非英文字母。
实例:
NumOfString.java:
程序代码
public class NumOfString
{
public static void main(String args[]) {
String s = "1jkhKhjklhklHLHjb90kBNo9" ;
System.out.println("***********方法一***********") ;
numChar1(s) ;
System.out.println("***********方法二***********") ;
numChar2(s) ;
System.out.println("***********方法三***********") ;
numChar3(s) ;
}
public static void numChar1(String s) {
int cU = 0 ;
int cL = 0 ;
int cN = 0 ;
char c ;
for(int i=0 ;i<s.length() ;i++) {
c = s.charAt(i) ;
if(c > 'a' && c < 'z') {
cL ++ ;
}
else if(c > 'A' && c < 'Z') {
cU ++ ;
}
else {
cN ++ ;
}
}
System.out.println("Number of LowerCase:" + cL) ;
System.out.println("Number of UpperCase:" + cU) ;
System.out.println("Number of NotChar:" + cN) ;
}
public static void numChar2(String s) {
String sL = "abcdefghijklmnopqrstuvwxyz" ;
String sU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
int cU = 0 ;
int cL = 0 ;
int cN = 0 ;
char c ;
for(int i=0 ; i<s.length() ;i++) {
c = s.charAt(i) ;
if(sL.indexOf(c) != -1) {
cL ++ ;
}
else if(sU.indexOf(c) != -1) {
cU ++ ;
}
else {
cN ++ ;
}
}
System.out.println("Number of LowerCase:" + cL) ;
System.out.println("Number of UpperCase:" + cU) ;
System.out.println("Number of NotChar:" + cN) ;
}
public static void numChar3(String s) {
int cU = 0 ;
int cL = 0 ;
int cN = 0 ;
char c ;
for(int i=0 ;i<s.length() ;i++) {
c = s.charAt(i) ;
if(Character.isLowerCase(c)) {
cL ++ ;
}
else if(Character.isUpperCase(c)) {
cU ++ ;
}
else {
cN ++ ;
}
}
System.out.println("Number of LowerCase:" + cL) ;
System.out.println("Number of UpperCase:" + cU) ;
System.out.println("Number of NotChar:" + cN) ;
}
}
从字符串中分离出字符,依次将每一个字符与'a','z'比较,判断是否为小写字母,与'A','Z'比较,判断是否为大写字母,其他的为非英文字母。
方法二:
从字符串中分离出字符,依次将每一个字符在'a'~'z'中查找,如果查到则为小写字母,同样方法判断是否为大写字母,其他的为非英文字母。
方法三:
从字符串中分离出字符,依次将每一个字符用Character.isLowerCase()方法判断是否为小写字母,Character.isUpperCase()判断是否为大写字母,其他的为非英文字母。
实例:
NumOfString.java:
![程序代码](http://mgc.ahau.edu.cn/p_w_picpaths/code.gif)
public class NumOfString
{
public static void main(String args[]) {
String s = "1jkhKhjklhklHLHjb90kBNo9" ;
System.out.println("***********方法一***********") ;
numChar1(s) ;
System.out.println("***********方法二***********") ;
numChar2(s) ;
System.out.println("***********方法三***********") ;
numChar3(s) ;
}
public static void numChar1(String s) {
int cU = 0 ;
int cL = 0 ;
int cN = 0 ;
char c ;
for(int i=0 ;i<s.length() ;i++) {
c = s.charAt(i) ;
if(c > 'a' && c < 'z') {
cL ++ ;
}
else if(c > 'A' && c < 'Z') {
cU ++ ;
}
else {
cN ++ ;
}
}
System.out.println("Number of LowerCase:" + cL) ;
System.out.println("Number of UpperCase:" + cU) ;
System.out.println("Number of NotChar:" + cN) ;
}
public static void numChar2(String s) {
String sL = "abcdefghijklmnopqrstuvwxyz" ;
String sU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
int cU = 0 ;
int cL = 0 ;
int cN = 0 ;
char c ;
for(int i=0 ; i<s.length() ;i++) {
c = s.charAt(i) ;
if(sL.indexOf(c) != -1) {
cL ++ ;
}
else if(sU.indexOf(c) != -1) {
cU ++ ;
}
else {
cN ++ ;
}
}
System.out.println("Number of LowerCase:" + cL) ;
System.out.println("Number of UpperCase:" + cU) ;
System.out.println("Number of NotChar:" + cN) ;
}
public static void numChar3(String s) {
int cU = 0 ;
int cL = 0 ;
int cN = 0 ;
char c ;
for(int i=0 ;i<s.length() ;i++) {
c = s.charAt(i) ;
if(Character.isLowerCase(c)) {
cL ++ ;
}
else if(Character.isUpperCase(c)) {
cU ++ ;
}
else {
cN ++ ;
}
}
System.out.println("Number of LowerCase:" + cL) ;
System.out.println("Number of UpperCase:" + cU) ;
System.out.println("Number of NotChar:" + cN) ;
}
}
转载于:https://blog.51cto.com/77857/175111