简体   繁体   English

字符串范围内的Java整数

[英]Java Integer in a String range

So I have a string specifying a valid range of numbers . 所以我有一个字符串,指定一个有效的数字范围。

for eg. 例如

String range = "100-200 , 500 , 1000-1200";

Need to check if a given integer value is within this specified range The range is different everytime. 需要检查给定的整数值是否在此指定范围内。该范围每次都不同。

You should first separate the different ranges. 您首先应该分开不同的范围。 The valid ranges appear to be space-comma-space separated, so you need to split by , . 有效范围似乎是空间逗号分隔的空间,所以你需要拆分的,

String[] ranges = range.split(" , ");

And then for each range you need to split it by a hyphen. 然后,对于每个范围,您都需要将其用连字符分隔。 As Stultuske pointed out , you have to take into account the fact that the hyphen itself could be part of the integer notation. 正如Stultuske指出的那样 ,您必须考虑到连字符本身可能是整数符号的一部分的事实。 If you expect only positive values, then you can split by a single hyphen: 如果只期望正值,则可以用单个连字符分开:

String rangeStr = "400-600";
String parts = rangeStr.split("-");

Alternatively, if you expect also negative values, then you should split by the first hyphen that is not at the start of the string, and apply the split only once. 或者,如果您还期望负值,则应按不在字符串开头的第一个连字符进行拆分,并仅应用拆分一次。

String rangeStr = "-1200--1000";
// Splits by hyphen which is not preceded by the start of the string.
// The resulting array has no more than two elements
String[] parts = rangeStr.split("(?<!^)-", 2);

Note that this also works if a single value is given instead of a range, eg 500 . 请注意,如果仅给出一个值而不是一个范围(例如500 ,这也将起作用。 Then, of course, parts.length is 1 . 然后,当然, parts.length1

And then you can get the ints by calling Integer.valueOf(...) . 然后,您可以通过调用Integer.valueOf(...)获得整数。


Here is the code wrapped in a static method: 这是包装在静态方法中的代码:

public static boolean inRange(String range, int value) {
    return Arrays.stream(range.split(" , "))
        .anyMatch(t -> {
            // Split the two integers denoting the range. The length of the
            // array is 1 if the range is an exact value (500 in your case),
            // otherwise, its length is 2
            String[] parts = t.split("(?<!^)-", 2);
            int minVal = Integer.valueOf(parts[0]);
            int maxVal = (parts.length == 2 ? Integer.valueOf(parts[1]) : minVal);
            return (minVal <= value && value <= maxVal);
        });
    }
}

And here some tests: 这里是一些测试:

String range = "-2100--2000, 100-200, 500, 1000-1200, 2000-2100";
System.out.println(inRange(range, -2150)); // false
System.out.println(inRange(range, -2000)); // true
System.out.println(inRange(range, 400));   // false
System.out.println(inRange(range, 500));   // true
System.out.println(inRange(range, 1100));  // true

Note that I have assumed that the source string is in the correct format. 请注意,我假设源字符串的格式正确。 If you want to be a little more tolerant with what source string you expect (for example trailing spaces of multiple spaces in between), you have to implement that yourself. 如果您想更宽容期望的源字符串(例如,中间的多个空格的尾部空格),则必须自己实现。

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

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