检查字符串中两个元素之间是否有空格
Checking if there is whitespace between two elements in a String
我正在处理字符串,我需要将两个 chars/elements 分开,如果它们之间有白色 space。我在 SO 上看到过一个前者 post,但它仍然没有按预期对我起作用。如您所想,我可以只检查字符串是否包含 (" "),然后检查 space 周围的子字符串。然而,尽管字符之间没有白色space,但我的字符串可能在末尾包含无数的白色space。因此我的问题是“如何检测两个字符(也是数字)之间的白色space”?
//字符串中包含数字的示例
String test = "2 2";
final Pattern P = Pattern.compile("^(\d [\d\d] )*\d$");
final Matcher m = P.matcher(test);
if (m.matches()) {
System.out.println("There is between space!");
}
您将使用 String.strip()
删除任何开头或结尾的白色 space,然后是 String.split()
。如果有whitespace,则数组长度为2或更大。如果没有,则长度为1。
示例:
String test = " 2 2 ";
test = test.strip(); // Removes whitespace, test is now "2 2"
String[] testSplit = test.split(" "); // Splits the string, testSplit is ["2", "2"]
if (testSplit.length >= 2) {
System.out.println("There is whitespace!");
} else {
System.out.println("There is no whitespace");
}
如果需要指定长度的数组,也可以指定一个limit进行拆分。例如:
"a b c".split(" ", 2); // Returns ["a", "b c"]
如果您想要一个只使用正则表达式的解决方案,以下正则表达式匹配由单个 space 分隔的任意两组字符,前导或尾随白色 space 任意数量:
\s*(\S+\s\S+)\s*
这是最简单的方法:
String testString = " Find if there is a space. ";
testString.trim(); //This removes all the leading and trailing spaces
testString.contains(" "); //Checks if the string contains a whitespace still
您还可以通过链接两个方法在一行中使用 shorthand 方法:
String testString = " Find if there is a space. ";
testString.trim().contains(" ");
如果您使用正则表达式 (?<=\w)\s(?=\w)
,则正向前瞻和后视也可能有效
\w
: 一个单词字符 [a-zA-Z_0-9]
\s
: 空格
(?<=\w)\s
:正后视,如果空格前面有 \w
则匹配
\s(?=\w)
:正面前瞻,如果空格后跟 \w
则匹配
List<String> testList = Arrays.asList("2 2", " 245 ");
Pattern p = Pattern.compile("(?<=\w)\s(?=\w)");
for (String str : testList) {
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(str + "\t: There is a space!");
} else {
System.out.println(str + "\t: There is not a space!");
}
}
输出:
2 2 : There is a space!
245 : There is not a space!
您的模式无法按预期工作的原因是因为 ^(\d [\d\d] )*\d$
可以简化为 (\d \d )*\d$
从重复括号之间的内容开始 0 次或多次。
然后匹配字符串末尾的数字。由于重复为 0 次或多次,它是可选的,它也将只匹配一个数字。
如果你想检查 2 个非白色 space 字符之间是否有单个 space:
\S \S
final Pattern P = Pattern.compile("\S \S");
final Matcher m = P.matcher(test);
if (m.find()) {
System.out.println("There is between space!");
}
使用
String text = "2 2";
Matcher m = Pattern.compile("\S\s+\S").matcher(text.trim());
if (m.find()) {
System.out.println("Space detected.");
}
text.trim()
将删除前导和尾随空格,\S\s+\S
模式匹配 non-whitespace,然后是一个或多个空格字符,然后再次是 non-whitespace 字符。
我正在处理字符串,我需要将两个 chars/elements 分开,如果它们之间有白色 space。我在 SO 上看到过一个前者 post,但它仍然没有按预期对我起作用。如您所想,我可以只检查字符串是否包含 (" "),然后检查 space 周围的子字符串。然而,尽管字符之间没有白色space,但我的字符串可能在末尾包含无数的白色space。因此我的问题是“如何检测两个字符(也是数字)之间的白色space”?
//字符串中包含数字的示例
String test = "2 2";
final Pattern P = Pattern.compile("^(\d [\d\d] )*\d$");
final Matcher m = P.matcher(test);
if (m.matches()) {
System.out.println("There is between space!");
}
您将使用 String.strip()
删除任何开头或结尾的白色 space,然后是 String.split()
。如果有whitespace,则数组长度为2或更大。如果没有,则长度为1。
示例:
String test = " 2 2 ";
test = test.strip(); // Removes whitespace, test is now "2 2"
String[] testSplit = test.split(" "); // Splits the string, testSplit is ["2", "2"]
if (testSplit.length >= 2) {
System.out.println("There is whitespace!");
} else {
System.out.println("There is no whitespace");
}
如果需要指定长度的数组,也可以指定一个limit进行拆分。例如:
"a b c".split(" ", 2); // Returns ["a", "b c"]
如果您想要一个只使用正则表达式的解决方案,以下正则表达式匹配由单个 space 分隔的任意两组字符,前导或尾随白色 space 任意数量:
\s*(\S+\s\S+)\s*
这是最简单的方法:
String testString = " Find if there is a space. ";
testString.trim(); //This removes all the leading and trailing spaces
testString.contains(" "); //Checks if the string contains a whitespace still
您还可以通过链接两个方法在一行中使用 shorthand 方法:
String testString = " Find if there is a space. ";
testString.trim().contains(" ");
如果您使用正则表达式 (?<=\w)\s(?=\w)
\w
: 一个单词字符[a-zA-Z_0-9]
\s
: 空格(?<=\w)\s
:正后视,如果空格前面有\w
则匹配
\s(?=\w)
:正面前瞻,如果空格后跟\w
则匹配
List<String> testList = Arrays.asList("2 2", " 245 ");
Pattern p = Pattern.compile("(?<=\w)\s(?=\w)");
for (String str : testList) {
Matcher m = p.matcher(str);
if (m.find()) {
System.out.println(str + "\t: There is a space!");
} else {
System.out.println(str + "\t: There is not a space!");
}
}
输出:
2 2 : There is a space!
245 : There is not a space!
您的模式无法按预期工作的原因是因为 ^(\d [\d\d] )*\d$
可以简化为 (\d \d )*\d$
从重复括号之间的内容开始 0 次或多次。
然后匹配字符串末尾的数字。由于重复为 0 次或多次,它是可选的,它也将只匹配一个数字。
如果你想检查 2 个非白色 space 字符之间是否有单个 space:
\S \S
final Pattern P = Pattern.compile("\S \S");
final Matcher m = P.matcher(test);
if (m.find()) {
System.out.println("There is between space!");
}
使用
String text = "2 2";
Matcher m = Pattern.compile("\S\s+\S").matcher(text.trim());
if (m.find()) {
System.out.println("Space detected.");
}
text.trim()
将删除前导和尾随空格,\S\s+\S
模式匹配 non-whitespace,然后是一个或多个空格字符,然后再次是 non-whitespace 字符。