java(3)如何判断字符串是否包括字母?

首先可以通过转换每个字符串成它的ASCII码,因为这个码是唯一的,而且是一一对应

a-z(48-57);A-Z(97-122);0-9(48-57);(下面有具体的表,可以自己查阅)

这时我们需要把字符串转换成ASCII码,通过比较这个数字是否在48-57和97-122的区间即可

转换的方式为:把char直接转换成int,得到的int就是码值(常用)

// 方法一:将char直接转化为int,其值就是字符的ascii
int byteAscii1 = (int) ch;
System.out.println(byteAscii1);

// 方法二:将char强制转换为byte
char ch = 'A';
byte byteAscii = (byte) ch;
System.out.println(byteAscii);

----------------------------------------------------------------------------------------------------------------

把ASCII码转换成相应的字符。直接强制转换int到char即可

// 直接int强制转换为char
int ascii = 65;
char ch1 = (char)ascii;
System.out.println(ch1);

----------------------------------------------------------------------------------------------------------------

下面是一个应用了该知识点的例题:

 Validate names: Write a program to read and validate names that are in form of given name(s) and surname, all along a line. The names are composed of letters and cannot include numbers and punctuation characters. If the input string is not a valid name, display the string and then comment that this is not a valid name; otherwise, add the valid name into the array of strings and then print the whole list of names in the array

验证名称:编写一个程序来读取和验证一行中给定名称和姓氏形式的名称。名称由字母组成,不能包含数字和标点字符。如果输入字符串不是有效名称,请显示该字符串,然后注释该名称无效;否则,请将有效名称添加到字符串数组中,然后打印数组中的整个名称列表。

(下面是代码,代码的注意事项在文末有标出)

		Scanner input= new Scanner(System.in);
		System.out.println("input");
		String result = ""; //记录输出结果,也可以使用string的数组,但是比较麻烦
        
        //接受数据
		String inputUser = input.nextLine();
        //通过空格把string变成string[]
		String [] ss = inputUser.split("\\s+");

        //遍历该string[]
		for(int j = 0; j < ss.length;j++)
		{
            //用来判断是否是有效字符的标志,true是有效;false是无效;
            //因为if后的条件所以默认是true
			boolean flag = true ;

			//遍历该string的每个字符;
			for(int i = 0 ; i < ss[j].length(); i++)
			{
				//转换成ASCII码
				int byteAscii1 = (int) ss[j].charAt(i);

                   //判断是否在A-Z和a-z之外
				if( (byteAscii1 < 65) || (byteAscii1 > 122) || ((byteAscii1 > 90) && (byteAscii1 < 97)))
				{
					flag = false;
                    break;
				}
			}

			//输出
			if(flag)
			{
                //添加到string中,等待输出
				result = result + " " + ss[j];
			}
			else
			{
				System.out.println("输入的字符"+ ss[j] +"无效");
			}
			

		}
		
		System.out.println("有效字符是" + result);

输出:

NB:(1)注意ascii和int的转换

        (2)if( (byteAscii1 < 65) || (byteAscii1 > 122) || ((byteAscii1 > 90) && (byteAscii1 < 97))) 这句话是判断该字符是否在a-z 和 A-Z之外,因此我默认的flag是true。

        (3)我之前写的是if( (byteAscii1 < 65) || (byteAscii1 > 122) || (byteAscii1 >  90)  ||  (byteAscii1 < 97)) ;但是这是有问题的,因为如果这么判断,那么aaa就是错误的;a是97,得到的结果是: FFTF= T;因为这样取交集,不是我要的在48-57 和97-122; 

         (4)还有我之前也这么写过:

boolean flag = false;

if(((byteAscii1 > 64) && (byteAscii1 < 91) ) || ((byteAscii1 >  96)  &&  (byteAscii1 <  123))) 

{

           flag = true;

}

这样也是不对的,因为他无法处理00ff的情况,为什么呢?

这个if条件里面确实是正确的,但是执行00的时候,是不进入if的,所以默认就是false的,但是当遍历到ff的时候,就会进入if循环,就会flag 变成true,所以最后的结果也就是true,但其实他的无效字符。导致他的本质原因是,我这里是遍历的每个string的每个字符,但是却使用了一个flag,如果使用的是flag数组,这么写可以,但是在判断是否有效无效的时候在处理一下就可以了,但是如果我就想用一个flag怎么做呢,就只能默认他是正确的,发现一个字符不满足题意改成false。

----------------------------------------------------------------------------------------------------------------

下面是对应的ASCII码

----------------------------------------------------------------------------------------------------------------

有个思考?

如何获得null的ASCII值?

方法一:

报错:c cannot be resolved to a variable

代码:

	char s ;
	int a = (int) c;
	System.out.print(a);

方法二:

报错:Invalid character constant

代码:

char s ='';
int a = (int) c;
System.out.print(a);

因此目前作者还不知如何获得这个的值。如果读者知道请留言,告知谢谢!!

----------------------------------------------------------------------------------------------------------------

参考:https://www.cnblogs.com/ooo0/p/8465237.html

https://blog.csdn.net/JinChao94/article/details/81866476

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值