- Java.lang 包类
- Java.lang - 首页
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包额外内容
- Java.lang - 接口
- Java.lang - 错误
- Java.lang - 异常
- Java.lang 包实用资源
- Java.lang - 实用资源
- Java.lang - 讨论
Java - Character isSpaceChar() 方法
描述
Java 的 Character isSpaceChar() 方法用于判断指定的字符是否为 Unicode 空格字符。当且仅当 Unicode 标准将其指定为空格字符时,才认为该字符为空格字符。
如果字符的通用类别类型属于以下任何一种,则此方法返回 true:
SPACE_SEPARATOR
LINE_SEPARATOR
PARAGRAPH_SEPARATOR
语法
以下是 Java Character isSpaceChar() 方法的语法:
public static boolean isSpaceChar(char ch) (or) public static boolean isSpaceChar(int codePoint)
参数
ch − 要测试的字符
codePoint − 要测试的 Unicode 码点
返回值
如果字符为空格字符,则此方法返回 true,否则返回 false。
检查字符是否为空格字符的示例
以下示例演示了 Java Character isSpaceChar(char ch) 方法的使用。我们用两个字符值初始化了两个字符变量。使用 isSpaceChar() 方法,我们检查字符变量是否包含空格字符。然后打印结果。
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { // create 2 char primitives ch1, ch2 char ch1, ch2; // assign values to ch1, ch2 ch1 = ' '; ch2 = '\u2028'; // LINE_SEPARATOR // create 2 boolean primitives b1, b2 boolean b1, b2; /** * check if ch1, ch2 are space characters * and assign results to b1, b2 */ b1 = Character.isSpaceChar(ch1); b2 = Character.isSpaceChar(ch2); String str1 = "ch1 is a space character is " + b1; String str2 = "ch2 is a space character is " + b2; // print b1, b2 values System.out.println( str1 ); System.out.println( str2 ); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
ch1 is a space character is true ch2 is a space character is true
检查码点是否为空格字符的示例
以下示例演示了 Java Character isSpaceChar(int codepoint) 方法的使用。我们用两个码点值初始化了两个整型变量。使用 isSpaceChar() 方法,我们检查整型变量是否包含空格字符。然后打印结果。
package com.tutorialspoint; public class CharacterDemo { public static void main(String[] args) { // create 2 int primitives cp1, cp2 int cp1, cp2; // assign values to cp1, cp2 cp1 = 0x2029; // PARAGRAPH_SEPARATOR cp2 = 0x1010; // create 2 boolean primitives b1, b2 boolean b1, b2; /** * check if cp1, cp2 represent space characters * and assign results to b1, b2 */ b1 = Character.isSpaceChar(cp1); b2 = Character.isSpaceChar(cp2); String str1 = "cp1 represents a space character is " + b1; String str2 = "cp2 represents a space character is " + b2; // print b1, b2 values System.out.println( str1 ); System.out.println( str2 ); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
cp1 represents a space character is true cp2 represents a space character is false
检查字符串是否包含空格字符的示例
下面的示例演示了如何检查字符序列或字符串是否包含空格字符。这是通过使用循环和条件语句完成的。
package com.tutorialspoint; public class SpaceCharDemo { public static void main(String []args){ String s = "Hello World"; int count = 0; for(int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if(Character.isSpaceChar(ch)) { System.out.println("The Space char is found at index " + i); count = count + 1; } } if(count == 0) System.out.println("The Space char is not found"); } }
输出
编译并运行上述程序后,将打印以下输出:
The Space char is found at index 5
检查包含换行符的字符串是否包含空格字符的示例
在另一个示例中,我们将输入一个包含换行符码点的字符序列,并使用此方法查找换行符位于哪个索引。
package com.tutorialspoint; public class SpaceCharDemo { public static void main(String []args){ String s = "Tutorials" + '\u2028' + "point"; for(int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if(Character.isSpaceChar((int)ch)) { System.out.println("The Space char is found at index " + i); } } } }
输出
编译并运行程序后,将获得以下输出:
The Space char is found at index 9
java_lang_character.htm
广告