Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

How to check whether a string contains at least one alphabet in java?

I want such a validation that My String must be contains at least one alphabet.

I am using the following:

String s = "111a11";
boolean flag = s.matches("%[a-zA-Z]%");

flag gives me false even though a is in my string s

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use .*[a-zA-Z]+.* with String.matches() method.

boolean atleastOneAlpha = s.matches(".*[a-zA-Z]+.*");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...