求 比较两个String(允许通配符)是否相等的代码 (50分)

  • 主题发起人 主题发起人 flai
  • 开始时间 开始时间
F

flai

Unregistered / Unconfirmed
GUEST, unregistred user!
例如
String str1="abcde";
String str2="a%d_";

比较结果应该是相等,其中%通配多个字符,_通配一个字符

是否有现成代码?
先行谢过!
 
现有字符串比较函数
自己找吧
CompareStr
CompareText
AnsiCompareStr
AnsiCompareText
AnsiSameStr
AnsiSameText
AnsiStrComp
AnsiStrIComp
AnsiStrLComp
AnsiStrLIComp
StrComp
StrIComp
StrLComp
StrLIComp
 
这个要自己写代码。。。
 
//There is a method using regular expression.
//usage: java CompareStr ab%cd_ abedgdfdfcd0
import java.util.regex.*;
public class CompareStr {
public static void main(String[] args) {
//to store your two strings.
String compare = args[0],
beCompare = args[1];

//translate your compare string to a regular expression.
String regex = compare.replaceAll("%", ".+").replaceAll("_", ".{1}");
System.out.println(regex);

//equal is true,or false.
boolean ifEqual = Pattern.matches(regex, beCompare);

System.out.println( compare + " equal to " + beCompare + " is: " + ifEqual);
}
}
//I'm a english learner,please correct my language error when correcting
//my programming error.
 
jdk 1.4 里好象有正则表式的包
 
后退
顶部