如何将Java句子分为几个部分? [英] How to divide a sentence into parts Java?

查看:117
本文介绍了如何将Java句子分为几个部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将类似"He and his brother playing football."的句子分成类似"He and""and his""his brother""brother playing""playing football"的几部分.使用Java可以做到吗?

How can I divide a sentence like "He and his brother playing football." into few part like "He and", "and his", "his brother", "brother playing" and "playing football" . Is it possible to do that by using Java?

推荐答案

假定单词"始终用单个空格分隔.使用String.split()

Assuming the "words" are always separated by a single space. Use String.split()

String[] words = "He and his brother playing football.".split("\\s+");
for (int i = 0, l = words.length; i + 1 < l; i++)
        System.out.println(words[i] + " " + words[i + 1]);

这篇关于如何将Java句子分为几个部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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