How to check if a string contains a substring containing spaces?

前端 未结 6 1764
一个人的身影
一个人的身影 2020-12-18 12:26

Say I have a string like this in java:

\"this is {my string: } ok\"

Note, there can be any number of white spaces in between the various characters. How do I

6条回答
  •  隐瞒了意图╮
    2020-12-18 13:25

    If you have any number of white space between each character of your matching string, I think you are better off removing all white spaces from the string you are trying to match before the search. I.e. :

    String searchedString = "this is {my string: } ok";
    String stringToMatch = "{my string: }";
    boolean foundMatch = searchedString.replaceAll(" ", "").contains(stringToMatch.replaceAll(" ",""));
    

提交回复
热议问题