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

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

问题描述

如何检查字符串是否包含空格字符,空格或。如果可能,请提供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;

推荐答案

要检查字符串是否包含空格,请使用 Matcher 并调用它的find方法。

For checking if a string contains whitespace use a Matcher and call it's find method.

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

如果你想检查是否只包含空格那么你可以使用 String.matches

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

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

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

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