字符串大小比较 String a ="5<w<10"; //也有可能是 ≤ a.replaceAll("w", "6");//w替换为6如果在w在5-10范围返回true,否则返回false如何实现?谢谢帮帮! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 数据库读出来是string型号的,肯定是有原因的我才这样做! public static void main(String args[]) { String num = "10"; String a = "5<w≤10"; System.out.println(check(a, num)); num = "5"; a = "-2< w ≤ 3"; System.out.println(check(a, num)); } public static boolean check(String a, String num){ boolean pass = true; String reg = "^(-?\\d+)([^\\d-]+)(-?\\d+)([^\\d-]+)(-?\\d+)$"; a = a.replaceAll("\\s", "").replaceAll("w", num); Matcher macther = Pattern.compile(reg).matcher(a); if(macther.find()){ if(macther.group(2).equals("<")){ pass = pass && (Integer.parseInt(macther.group(1)) < Integer.parseInt(macther.group(3))); }else if(macther.group(2).equals("≤")){ pass = pass && (Integer.parseInt(macther.group(1)) <= Integer.parseInt(macther.group(3))); } if(macther.group(4).equals("<")){ pass = pass && (Integer.parseInt(macther.group(3)) < Integer.parseInt(macther.group(5))); }else if(macther.group(4).equals("≤")){ pass = pass && (Integer.parseInt(macther.group(3)) <= Integer.parseInt(macther.group(5))); } }else{ pass = false; } return pass; } 请问如下是什么意思`谢谢`你的这个可以用` String reg = "^(-?\\d+)([^\\d-]+)(-?\\d+)([^\\d-]+)(-?\\d+)$"; double的不可以的`public static void main(String[] args){ String num = "51.0"; String a = "50.00<w≤1000"; System.out.println(check(a,num)); } 考虑了小数: public static void main(String args[]) { String num = "10"; String a = "5<w≤10"; System.out.println(check(a, num)); System.out.println(); num = "5"; a = "-2< w ≤ 3"; System.out.println(check(a, num)); System.out.println(); num = "5"; a = "4.9<w≤10.123"; System.out.println(check(a, num)); System.out.println(); } public static boolean check(String a, String num){ boolean pass = true; String reg = "^(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)$"; a = a.replaceAll("\\s", "").replaceAll("w", num); Matcher macther = Pattern.compile(reg).matcher(a); if(macther.find()){ if(macther.group(3).equals("<")){ pass = pass && (Double.parseDouble(macther.group(1)) < Double.parseDouble(macther.group(4))); }else if(macther.group(3).equals("≤")){ pass = pass && (Double.parseDouble(macther.group(1)) <= Double.parseDouble(macther.group(4))); } if(macther.group(6).equals("<")){ pass = pass && (Double.parseDouble(macther.group(4)) < Double.parseDouble(macther.group(7))); }else if(macther.group(6).equals("≤")){ pass = pass && (Double.parseDouble(macther.group(4)) <= Double.parseDouble(macther.group(7))); } }else{ pass = false; } return pass; } 正则表达式:"^(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)$"^ 字符串开始$ 字符串结束(-?\\d+(\\.\\d+)?) 匹配数字(包括小数)([^\\d-\\.]+) 匹配不是以下3种符号的内容:所有数字 - . 匹配结束后,用 group(x) 把值找出来进行比较 package test5;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test3 { private static final Pattern p = Pattern.compile("\\s*(\\S+)\\s*([<≤]).*?([<≤])\\s*(\\S+)\\s*"); public static void main(String[] arg) { String a = "5e-10 < w ≤ 6e4"; //≤ System.out.println(new Test3().check(a, 100.201)); // true System.out.println(new Test3().check(a, -100.201)); // false } public boolean check(String express, double value) { Matcher m = p.matcher(express); if (!m.matches()) { throw new IllegalArgumentException("表达式格式不正确"); } double min = 0.0d; double max = 0.0d; try { min = Double.parseDouble(m.group(1)); max = Double.parseDouble(m.group(4)); } catch (NumberFormatException e) { throw new IllegalArgumentException("数据格式不对"); } if ("<".equals(m.group(2))) { if ("<".equals(m.group(3))) { return (min < value) && (value < max); } else { return (min < value) && (value <= max); } } else { if ("<".equals(m.group(3))) { return (min <= value) && (value < max); } else { return (min <= value) && (value <= max); } } }} 先把<和<=都replace成“,”然后根据“,”split,得到3个字符串再parse字符串取到3个数字a,b,c,最后在进行判断。如果出现a==b或b==c,那再回头去判断<和<= Jtable加入到了Jscrollpane中 为啥不显示列名啊 深航java面试题,大家来看看呀? 查询数据 awt产生图片的问题? Java程序员在北京找工作几天未果,希望朋友们给指点介绍一下! 一个关于JXTA Shell的问题(懂JXTA的进) Applet如何在英文操作系统下显示中文? 如何用java实现Excel表格(具有合并拆分计算等功能)? java中如何返回数组??急! 如何使java_swing图形程序运行时不出现dos窗口? GUI的那个exitbutton gui 文本查找~~
public static void main(String args[]) {
String num = "10";
String a = "5<w≤10";
System.out.println(check(a, num));
num = "5";
a = "-2< w ≤ 3";
System.out.println(check(a, num));
}
public static boolean check(String a, String num){
boolean pass = true;
String reg = "^(-?\\d+)([^\\d-]+)(-?\\d+)([^\\d-]+)(-?\\d+)$";
a = a.replaceAll("\\s", "").replaceAll("w", num);
Matcher macther = Pattern.compile(reg).matcher(a);
if(macther.find()){
if(macther.group(2).equals("<")){
pass = pass && (Integer.parseInt(macther.group(1)) < Integer.parseInt(macther.group(3)));
}else if(macther.group(2).equals("≤")){
pass = pass && (Integer.parseInt(macther.group(1)) <= Integer.parseInt(macther.group(3)));
}
if(macther.group(4).equals("<")){
pass = pass && (Integer.parseInt(macther.group(3)) < Integer.parseInt(macther.group(5)));
}else if(macther.group(4).equals("≤")){
pass = pass && (Integer.parseInt(macther.group(3)) <= Integer.parseInt(macther.group(5)));
}
}else{
pass = false;
}
return pass;
}
String reg = "^(-?\\d+)([^\\d-]+)(-?\\d+)([^\\d-]+)(-?\\d+)$";
String num = "51.0";
String a = "50.00<w≤1000";
System.out.println(check(a,num)); }
String num = "10";
String a = "5<w≤10";
System.out.println(check(a, num));
System.out.println();
num = "5";
a = "-2< w ≤ 3";
System.out.println(check(a, num));
System.out.println();
num = "5";
a = "4.9<w≤10.123";
System.out.println(check(a, num));
System.out.println();
}
public static boolean check(String a, String num){
boolean pass = true;
String reg = "^(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)$";
a = a.replaceAll("\\s", "").replaceAll("w", num);
Matcher macther = Pattern.compile(reg).matcher(a);
if(macther.find()){
if(macther.group(3).equals("<")){
pass = pass && (Double.parseDouble(macther.group(1)) < Double.parseDouble(macther.group(4)));
}else if(macther.group(3).equals("≤")){
pass = pass && (Double.parseDouble(macther.group(1)) <= Double.parseDouble(macther.group(4)));
}
if(macther.group(6).equals("<")){
pass = pass && (Double.parseDouble(macther.group(4)) < Double.parseDouble(macther.group(7)));
}else if(macther.group(6).equals("≤")){
pass = pass && (Double.parseDouble(macther.group(4)) <= Double.parseDouble(macther.group(7)));
}
}else{
pass = false;
}
return pass;
}
"^(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)([^\\d-\\.]+)(-?\\d+(\\.\\d+)?)$"^ 字符串开始
$ 字符串结束
(-?\\d+(\\.\\d+)?) 匹配数字(包括小数)
([^\\d-\\.]+) 匹配不是以下3种符号的内容:所有数字 - . 匹配结束后,用 group(x) 把值找出来进行比较
package test5;import java.util.regex.Matcher;
import java.util.regex.Pattern;public class Test3 {
private static final Pattern p = Pattern.compile("\\s*(\\S+)\\s*([<≤]).*?([<≤])\\s*(\\S+)\\s*"); public static void main(String[] arg) {
String a = "5e-10 < w ≤ 6e4"; //≤
System.out.println(new Test3().check(a, 100.201)); // true
System.out.println(new Test3().check(a, -100.201)); // false
} public boolean check(String express, double value) {
Matcher m = p.matcher(express);
if (!m.matches()) {
throw new IllegalArgumentException("表达式格式不正确");
}
double min = 0.0d;
double max = 0.0d;
try {
min = Double.parseDouble(m.group(1));
max = Double.parseDouble(m.group(4));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("数据格式不对");
}
if ("<".equals(m.group(2))) {
if ("<".equals(m.group(3))) {
return (min < value) && (value < max);
} else {
return (min < value) && (value <= max);
}
} else {
if ("<".equals(m.group(3))) {
return (min <= value) && (value < max);
} else {
return (min <= value) && (value <= max);
}
}
}
}