怎样判断数字字符串中是否包含字母?

wangya55 2009-04-26 07:25:06
例如

string str=request.getParameter("textfield");

现在如何判断这个str中是否含有字母,以及怎么样判断最后一个字符是否是X
...全文
2668 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangya55 2009-04-26
  • 打赏
  • 举报
回复
谢谢你啦
wangya55 2009-04-26
  • 打赏
  • 举报
回复
谢谢啦
wangya55 2009-04-26
  • 打赏
  • 举报
回复
谢谢各位大哥了,解决了,呵呵
possibleonline 2009-04-26
  • 打赏
  • 举报
回复

public static void main(String[] args) {
// TODO Auto-generated method stub
String str="中国d中国x";
String regex="[^a-z]*[a-z]+[^a-z]*x";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(str);
if(m.matches()){
System.out.println(true);
}else{
System.out.println(false);
}
}
wcwtitxu 2009-04-26
  • 打赏
  • 举报
回复

// 数字字母混合,我测试了,可以啊
String str = "0sd00e23s";
if (str != null && Pattern.compile("(?i)[a-z]").matcher(str).find()) {
// 有字母
System.out.println("YES");
}
JamesLiu 2009-04-26
  • 打赏
  • 举报
回复
用正则表达式
if (str != null &&
str.matches("(?i)[^a-z]*[a-z]+[^a-z]*")) {
}
jinxfei 2009-04-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wangya55 的回复:]
if (str != null && Pattern.compile("(?i)[a-z]").matcher(str2).find()) {


这里面的pattern 是哪里的?
[/Quote]

java.util.regex.Pattern
cwjieNo1 2009-04-26
  • 打赏
  • 举报
回复
正则表达式学的不好;

public boolean hasAlpha(String s){
char[] string = s.toCharArray();
for(int i=0; i<string.length; i++){
if(Character.isLetter(string[i])){
return true;
}
}
return false;
}

wangya55 2009-04-26
  • 打赏
  • 举报
回复
不行的,这样的话只能检测全部的字符串是字母字符串,如果是字母和数字混合的根本没有用的啊
wangya55 2009-04-26
  • 打赏
  • 举报
回复
if (str != null && Pattern.compile("(?i)[a-z]").matcher(str2).find()) {


这里面的pattern 是哪里的?
wangya55 2009-04-26
  • 打赏
  • 举报
回复
我试试
wcwtitxu 2009-04-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wcwtitxu 的回复:]
if (str != null && str.matches("(?i)[^a-z]*[a-z]+[^a-z]*")) {
// 有字母
}
[/Quote]

我上面这回复错的..


if (str != null && Pattern.compile("(?i)[a-z]").matcher(str2).find()) {
// 有字母
}
wcwtitxu 2009-04-26
  • 打赏
  • 举报
回复
if (!str != null && str.endsWith("X")) {
// 以 X 结束
}
猿敲月下码 2009-04-26
  • 打赏
  • 举报
回复
	String str="sdfsdfx";
String str2="sdfsf123z";
System.out.println(str.matches("[a-zA-Z]*"));
System.out.println(str2.matches("[a-zA-Z]*"));

System.out.println(str.endsWith("x"));
System.out.println(str2.endsWith("x"));
wcwtitxu 2009-04-26
  • 打赏
  • 举报
回复
if (str != null && str.matches("(?i)[^a-z]*[a-z]+[^a-z]*")) {
// 有字母
}

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧