可以考虑用String的方法
public String[] split(String regex)Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
The string "boo:and:foo", for example, yields the following results with these expressions:
Regex Result
: { "boo", "and", "foo" }
o { "b", "", ":and:f" }
Parameters:
regex - the delimiting regular expression
Returns:
the array of strings computed by splitting this string around matches of the given regular expression
Throws:
PatternSyntaxException - if the regular expression's syntax is invalid
NullPointerException - if regex is null
Since:
1.4
See Also:
Pattern
/*
* @(#) Test.java
* Create By James Fancy
*/
package jamesfancy;
public class Test {
public static void main(String[] args) {
String text = "Subject areas are optional but highly recommended for "
+ "models of twenty classes or more. The purpose of subject "
+ "areas is to provide a convenient aggregation of model "
+ "classes and to partition large models into smaller more "
+ "manageable subsets.\n"
+ "You could document following parts (for example, use case "
+ "model, class model, and interaction model) for every "
+ "subject areas, at the same time give a whole program "
+ "diagram and literary description.";
System.out.println(text);
String[] words = text.split("[,(). \t\n\r]{1,}");
int total = words.length;
int maxLength = 0;
int minLength = text.length();
int totalLength = 0;
for (int i = 0; i < total; ++i) {
System.out.println("[Word] " + words[i]);
int len = words[i].length();
if (maxLength < len) {
maxLength = len;
}
if (minLength > len) {
minLength = len;
}
totalLength += len;
}