we have a list of strings, the user must enter the minimum and maximum numbers, after which the program checks whether the string contains a number in the range from minimum to maximum

  • String.contains (...) . - post_zeew
  • @post_zeew, no good. - Qwertiy
  • @Qwertiy, Why? - post_zeew
  • @post_zeew, contains checks for the occurrence of a substring. And in the matter of numbers. There is a line Тут 123 что-то 888 с 65 числами . How does contains help determine whether there are numbers in strings in the range of 16 to 311 ? - Qwertiy
  • @Qwertiy, did you seriously think that the above method would solve the problem directly? Of course no. Well, there are cycles there and so on. But this is far from an optimal decision, and therefore it is not published as an answer. So of course you need to extract the numbers from the string, and so on. - post_zeew

1 answer 1

Regular expressions will most likely help you in your question. Regular expressions (eng. Regular expressions) is a formal language for searching and manipulating with interlaces in the text, based on the use of metacharacters (jokers, English wildcard characters). In essence, this is a sample string (English pattern, in Russian it is often called a “pattern”, “mask”), consisting of characters and metacharacters and specifying a search rule.

  import java.util.regex.*; String msg = "Строка с цифрами 12345"; Pattern pattern = Pattern.compile("[0-9]+"); Matcher matcher = pattern.matcher(msg); while(matcher.find()){ Integer result = new Integer(msg.substring(matcher.start(),matcher.end())); if(result > 100) System.out.println( result + " > 100" ); }