Home > Java > JavaBase > body text

How to determine whether char is a space in java

王林
Release: 2019-11-28 09:22:39
Original
7133 people have browsed it

How to determine whether char is a space in java

函数介绍:

toCharArray() 方法将字符串转换为字符数组。

语法:

public char[] toCharArray()
Copy after login

返回值:

字符数组。

在线视频教程推荐:java学习视频

判断思路:

首先通过调用“toCharArray”方法将需要判断的字符串保存到字符数组中,然后使用“for”循环判断字符数组的长度,通过长度的判断来确定“char”是否是空格。

示例如下:

//判断是否char是否为空
import java.util.*;
public class test{ 
 public static void main(String[] args){ 
 String str="abc abc";
 char [] ch=str.toCharArray();
 for(int i=0;i<ch.length;i++) {
     if(Character.isSpace(ch[i])){
         System.out.println("ok");
     }
 } 
 }
Copy after login

扩展知识:

//判断char是否为空字符串         
  char M=&#39;A&#39;;
            if(M!=&#39;\0&#39;){
                System.out.println("字符M不为空");
            }    
            
            char[] N=new char[1];
            if(N[0]==&#39;\0&#39;)
                System.out.println("字符N[0]为空\n\n\n\n");
Copy after login

相关文章教程推荐:java入门程序

The above is the detailed content of How to determine whether char is a space in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!