简体   繁体   English

Java中的字符串比较,不带空格

[英]String comparison in Java without spaces

Folks, 伙计们,

I am having a little doubt with String comparison (with blank spaces in between) in Java . 我对Java中的String比较(中间有空格)有些怀疑。

UseCase :- 用例:-
Consider the following Solr query string :- 考虑以下Solr查询字符串:

  String query = ".....&ansDt:[* TO *]....";

Valid format for ansDt are :- ansDt的有效格式为:-

  1. ansDt : [* TO *]
  2. ansDt : [someDate TO *]
  3. ansDt : [* TO someDate]
  4. ansDt : [someDate TO otherDate]

I have two doubts:- 我有两个怀疑:

  • Now, I need a way to check the query string for only 4th ansDt format. 现在,我需要一种方法来仅检查4th ansDt格式的查询字符串。 If other format is encountered ( in short, if any * pattern is encountered in ansDt field) , I need to throw some message on console. 如果遇到其他格式(简而言之,如果在ansDt字段中遇到任何*模式),我需要在控制台上抛出一些消息。

  • Also I need to check for 1st, 2nd and 3rd case irrespective of blank spaces between ansDt & : & [ . 另外,无论ansDt:[之间有空格,我都需要检查第一种, ansDt和第三种情况。 ie ansDt: [* TO *] , ansDt :[* TO *] , ansDt : [ * TO *] etc are all considered same ansDt: [* TO *]ansDt :[* TO *]ansDt : [ * TO *]等均被视为相同

I cannot use String.trim( ) because it will remove only the leading and ending whitespaces for a string 我不能使用String.trim( )因为它将仅删除字符串的开头和结尾空格

How do I check that with Java String comparison ? 如何使用Java字符串比较进行检查?

Thanks 谢谢

Please try this regex: 请尝试此正则表达式:

String query = ".....&ansDt:[* TO *]....";

String pattern = ".*ansDt[\\ ]*[:][\\ ]*[\\[].*[\\ ]+TO[\\ ]+.*[\\]].*"; 

boolean test = query.matches(pattern);

Additional info: 附加信息:

. matches any character

.* matches any amount of characters

[\ ] matches space

[\ ]+ matches one or more spaces, used between "TO"

[\ ]* matches any amount of spaces, used between ":"

[\[] and [\]] matches "[" and "]"

Regex is the way to go. 正则表达式是必经之路。 But for a quick fix you can try 但是为了快速解决问题,您可以尝试

    String query = ".....&ansDt : [ * TO *]...."; 
    int indexOfansDt = query.indexOf("ansDt");
    int indexOfBeginBracket = query.indexOf("[",indexOfansDt);
    int indexOfEndBracket = query.indexOf("]",indexOfBeginBracket);
    String yourString = query.substring(indexOfBeginBracket, indexOfEndBracket+1);
    System.out.println(yourString);

Check yourString for indexOf("*") , if it is not -1 your format is 1,2 or 3. But there are a lot of error cases, NPEs that you have to check for. 检查yourStringindexOf("*") ,如果不是-1 ,则格式为1,2或3。但是有很多错误情况,您必须检查NPE。

You'll need something more complex than a simple regex. 您将需要比简单的正则表达式更复杂的东西。 You've written what amounts to a four line grammar. 您已经写了四行语法。 You should take the approach that a parser generator would: tokenize it and then execute rules to see if the tokens match your grammar. 您应该采用解析器生成器将采用的方法:将其标记化,然后执行规则以查看标记是否与您的语法匹配。

If you can stand the big hammer of a parser generator, I'd recommend ANTLR. 如果您能忍受解析器生成器的强大功能,我建议您使用ANTLR。

这应该做(对于字符串s1s2 ):

s1.replace(" ", "").equals(s2.replace(" "), ""))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM