如何在字符串中找到空格? [英] How can I find whitespace in a String?

查看:76
本文介绍了如何在字符串中找到空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查字符串是否包含空格字符,空格或".如果可能,请提供一个Java示例.

How can I check to see if a String contains a whitespace character, an empty space or " ". If possible, please provide a Java example.

例如: String ="test word";

推荐答案

要检查字符串是否包含空格,请使用

For checking if a string contains whitespace use a Matcher and call its find method.

Pattern pattern = Pattern.compile("\\s");
Matcher matcher = pattern.matcher(s);
boolean found = matcher.find();

如果要检查它是否仅由空格组成,则可以使用

If you want to check if it only consists of whitespace then you can use String.matches:

boolean isWhitespace = s.matches("^\\s*$");

这篇关于如何在字符串中找到空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆