Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个数组
String[] grades = new String[]{"D","C-","C","C+","B-","B","B+","A-","A","A+"};
我想检查一个字符串是否是这些值之一。
我可以遍历数组来完成这个,但我想通过正则表达式来完成。
正则表达式相当简单:
[A-C][+-]?|D
第一部分说AthroughC后面可以跟一个可选的加号或减号;第二部分允许 aD本身。
A
C
D
我可以遍历数组来完成这个
您也可以使用contains(), 来执行此操作而无需循环:
contains()
if (Arrays.asList(grades).contains(grade)) { ... }