eg_1

1. 编写一个程序,输出一个字符串中的大写英文字母个数,小写英文字母个数以及非英文字母个数.

  第一种方法:

public class Test {
	public static void main(String[] args) {
		String s = "[email protected]&&&haAA";
		int Lcount = 0, Ucount = 0, Ocount = 0;
		for(int i=0;i<s.length();i++){
			if(s.charAt(i) >= ‘a‘ && s.charAt(i) <= ‘z‘){
				Lcount++;
			}
			else if(s.charAt(i) >= ‘A‘ && s.charAt(i) <= ‘Z‘){
				Ucount++;
			}
			else {
				Ocount++;
			}
		}
		System.out.println("Lcount: "+Lcount+","+"Ucount: "+Ucount+","+"Ocount: "+Ocount);
	}
} //Lcount: 22,Ucount: 10,Ocount: 17

  第二种方法:

public class Test {
	public static void main(String[] args) {
		String s = "[email protected]&&&haAA";
		int Lcount = 0, Ucount = 0, Ocount = 0;
		for(int i=0;i<s.length();i++){
			if(Character.isLowerCase(s.charAt(i))){
				Lcount++;
			}
			else if(Character.isUpperCase(s.charAt(i))){
				Ucount++;
			}
			else {
				Ocount++;
			}
		}
		System.out.println("Lcount: "+Lcount+","+"Ucount: "+Ucount+","+"Ocount: "+Ocount);
	}
}// Lcount: 22,Ucount: 10,Ocount: 17

第三种方法:

public class Test {
	public static void main(String[] args) {
		String BL = "abcdefghijklmnopqrstuvwxyz";
		String BU = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		String s = "[email protected]&&&haAA";
		int Lcount = 0, Ucount = 0, Ocount = 0;
		for(int i=0;i<s.length();i++){
			if(BL.indexOf(s.charAt(i)) != -1){
				Lcount++;
			}
			else if(BU.indexOf(s.charAt(i)) != -1){
				Ucount++;
			}
			else {
				Ocount++;
			}
		}
		System.out.println("Lcount: "+Lcount+","+"Ucount: "+Ucount+","+"Ocount: "+Ocount);
	}
}// Lcount: 22,Ucount: 10,Ocount: 17
时间: 2024-10-24 17:46:38

eg_1的相关文章

C# 反射小结

废话不多说,直接上代码. 1.typeof(类名):它是一个运算符 eg_1:Type type = typeof(int) ; eg_2:public class Student { Type type = typeof(Student) ; ................................................... } 2.object.GetType(类的完全限定名):它是一个方法 object 是具体实例时 eg:int iNumber=3 ; Type ty

拆分含有多种分隔符的字符串

# 拆分含有多种分隔符的字符串 """实际案例: 把某个字符串依据分割符号拆分不同的字段,该字段包含多种不同的分隔符 list0 = "ab;cd|efg|hi,jkl|mn\topq;rst,uvw\txyz" 其中 <,>,<;>,<|>,<\> 都是分隔符""" # 单一分隔符使用 s = 'www 8596 0.0 0.0 15572 2136 pts/1 R+ 15:5