gpt4 book ai didi

java - java中的空格

转载 作者:搜寻专家 更新时间:2023-10-30 20:58:36 25 4
gpt4 key购买 nike

Java 中的空格有哪些种类?如果文本包含任何空格,我需要检查我的代码。

我的代码是:

if (text.contains(" ") || text.contains("\t") || text.contains("\r") 
|| text.contains("\n"))
{
//code goes here
}

我已经知道 \n\t\rspace

最佳答案

对于非正则表达式的方法,你可以查看Character.isWhitespace对于每个字符。

boolean containsWhitespace(String s) {
for (int i = 0; i < s.length(); ++i) {
if (Character.isWhitespace(s.charAt(i)) {
return true;
}
}
return false;
}

Which are the white spaces in Java?

文档指定了 Java 认为什么是空白:

public static boolean isWhitespace(char ch)

Determines if the specified character is white space according to Java. A character is a Java whitespace character if and only if it satisfies one of the following criteria:

  • It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
  • It is '\u0009', HORIZONTAL TABULATION.
  • It is '\u000A', LINE FEED.
  • It is '\u000B', VERTICAL TABULATION.
  • It is '\u000C', FORM FEED.
  • It is '\u000D', CARRIAGE RETURN.
  • It is '\u001C', FILE SEPARATOR.
  • It is '\u001D', GROUP SEPARATOR.
  • It is '\u001E', RECORD SEPARATOR.
  • It is '\u001F', UNIT SEPARATOR.

关于java - java中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11863503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com