可以考虑用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
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
String[] result=source.split(" ");
单词可以以空格为分隔符依次截取为一个新的字符串
然后可以用.lenght来计算长度
public static void main(String[] args) {
int[] len = new int[args.length];
for (int i = 0; i < args.length; i++) {
len[i] = args[i].length();
System.out.println(len[i]);
}
}
}
* @(#) 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;
} System.out.println("单词总数:" + total);
System.out.println("最大长度:" + maxLength);
System.out.println("最小长度:" + minLength);
System.out.println("平均长度:" + (double) totalLength / total);
}}