根据没有apache库的空白将行拆分为字符串

根据没有apache库的空白将行拆分为字符串

问题描述:

我想将缓冲读取器中的一行分成基于它们之间的空白字符串的数组。根据没有apache库的空白将行拆分为字符串

BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
String line; 

我知道从Apache库,让你做的方法因此使用

String[] words = StringUtils.split(line); 

这是一个测试实践,我将无法访问任何外部库的。我如何仅使用JDK执行此操作。

你可以这样做:

String words[] = line.split(" "); 

公告拆分的呼叫内的空间。我希望这有帮助。

+0

谢谢。太棒了。我不知道为什么我错过了它。 – Sithe

使用String类的拆分方法: line.split(“\ s +”) 所有空格都将被视为分隔符。

内置的Java String类可以做到这一点。在这里阅读文档:

String Documentation

向下滚动到 “拆分(...)” 方法。