如何按空格分割字符串 [英] How to split a String by space

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

问题描述

我需要用空格分割我的字符串.为此,我尝试了:

I need to split my String by spaces. For this I tried:

str = "Hello I'm your String";
String[] splited = str.split(" ");

但它似乎不起作用.

推荐答案

你所拥有的应该有效.但是,如果提供的空格默认为...其他内容?您可以使用空白正则表达式:

What you have should work. If, however, the spaces provided are defaulting to... something else? You can use the whitespace regex:

str = "Hello I'm your String";
String[] splited = str.split("\\s+");

这将导致任意数量的连续空格将您的字符串拆分为标记.

This will cause any number of consecutive spaces to split your string into tokens.

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

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