【发布时间】:2017-03-16 01:14:10
【问题描述】:
例如, 字符串字母 = "fourgooddogsswam";
有没有一种方法可以一次从左到右扫描字符串 4 个字符,以便我可以将(四个 ourg urgo rgoo good oodd oddo ddog dogs ogss gssw sswa swam)设置为字符串数组? 我尝试使用循环,但很难让它正常工作。
谢谢!
public static String[] findWordsOfLength(String letters, int wordSize) {
if(letters == null) {
return null;
}
int size = letters.length();
int wordMax = size - wordSize + 1;
if(size < wordMax || wordMax <= 0) {
return new String[0];
}
int j = 0;
String[] result = new String[wordMax];
for (int i = 0; i < wordMax; i++) {
result[j ++] = letters.substring(i, i + wordSize);
}
return result;
}
【问题讨论】:
-
向我们展示您的尝试并解释您遇到的问题。
-
请显示您尝试过的循环。 SO 不是免费的编码服务。
-
我输入了我正在处理的内容,基本上我引入了一串字母并使用 wordSize int 来确定它为每个单词扫描字符串的字母数。 Fourgooddogsswam 示例使用了 4