我正在尝试将一个长字符串切成该字符串的行,行的长度由函数决定。该功能不会剪切中间的单词。
我已经尝试了许多使用子字符串之类的方法,但是我在字符串操作方面并不是那么出色。我在网上发现了一个类似的问题,但这是在JavaScript中,有些代码我无法完全转换为Java(也许是因为我对此没有经验……)
public static List<String> descriptionFormatter(String string, int amt)
{
String[] splitted = string.split(" ");
String part = "";
List<String> finalDesc = new ArrayList<String>();
for(int i = 0 ; i < splitted.length; i++)
{
part = part + " " + splitted[i];
if(part.length() >= amt)
{
finalDesc.add(part);
part = "";
}
}
return finalDesc;
}
例如 我有一个字符串“你好,世界苹果,橙色葡萄汁,spagehtti酱牛奶” 我想每34个字符剪切一次(考虑到以上所有要求) 所以我打电话
descriptionFormatter(string, 34);
想要结果字符串数组/列表:
你好世界苹果橙葡萄汁
spagehtti酱汁牛奶
实际结果:
我已经尽其所能,但是有时它会跳过其余单词的末尾,并在第一个单词之前放置空格。如何使其发挥预期的作用?
答案 0 :(得分:0)
尝试这样做
static List<String> split(String s, int size) {
return split(s.toCharArray(), size);
}
static List<String> split(char[] s, int size) {
List<String> strs = new ArrayList<>();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length; ++i) {
if (i % size == 0) {
if(s[i] == ' ') {
strs.add(sb.toString());
sb = new StringBuilder();
}else {
StringBuilder newStringBuilder = new StringBuilder();
int length = sb.length();
while (length > 0 && sb.charAt(length - 1) != ' ') {
newStringBuilder.insert(0, sb.charAt(length - 1));
sb.deleteCharAt(length - 1);
--length;
}
if(sb.length() > 0) strs.add(sb.toString());
sb = newStringBuilder;
}
}
sb.append(s[i]);
}
if (sb.length() > 0) {
strs.add(sb.toString());
}
return strs;
}
答案 1 :(得分:0)
public static List<String> descriptionFormatter(String string, int amt) {
List<String> stringPieces = new ArrayList<>();
StringBuilder strOfMaxLen = new StringBuilder();
StringBuilder strExceedsMaxLen = new StringBuilder();
String[] splitted = string.split(" ");
for (int i = 0 ; i < splitted.length; i++) {
String piece = splitted[i];
int pieceLen = piece.length();
if (strOfMaxLen.length()+pieceLen < amt) {
if (strOfMaxLen.length() != 0) {
strOfMaxLen.append(" ");
}
strOfMaxLen.append(piece);
} else {
if (strExceedsMaxLen.length() != 0) {
strExceedsMaxLen.append(" ");
}
strExceedsMaxLen.append(piece);
}
}
stringPieces.add(strOfMaxLen.toString());
stringPieces.add(strExceedsMaxLen.toString());
return stringPieces;
}
答案 2 :(得分:0)
还有另一个:)
[
55.50236554101884,
48.4420340522227,
48.936642078369324,
39.61167139428769,
39.308159841569264,
42.58440044993444,
43.68867735156312,
50.14296711032441,
50.83654805452586,
50.79841825933019,
50.311807765799145,
45.53568823545641,
43.2867452256146,
43.38390663365912,
41.2894895311203,
41.133921783119135,
41.957983467430424,
34.6763182163226,
29.239623192248875,
43.73473552102837,
39.70980937294976,
38.722071869417974,
33.91981286332463,
40.415256033334316,
39.967050108741674,
30.237053069193013,
47.24515019760144,
48.09365697810791,
47.397062335425126,
47.919330703443016,
48.50051589076493,
45.588023453959245,
41.35162036615853,
39.976389434938056,
41.79568536987075,
36.94666938984934,
40.95986916491035,
38.21570616480444,
38.538139356924304,
39.93113081247877,
39.69620754900941,
38.9256798286741,
34.87273047865816,
43.210471567071465,
37.873988767931046,
35.92186113528669,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
]
答案 3 :(得分:0)
您可以尝试遍历输入string
和两个索引,分别为beginIndex
和endIndex
,并在进行操作时从输入substrings
中提取string
:>
public static List<String> descriptionFormatter(String str, int amt) {
List<String> result = new ArrayList<>();
// trim the input string to avoid empty strings at the end
str = str.trim();
int beginIndex = 0;
int endIndex = amt;
final int length = str.length();
while(endIndex < length) {
// if we landed on something other than space
// increase the end index for the substring
// until we hit a space
while(endIndex < length && str.charAt(endIndex) != ' ') {
++endIndex;
}
result.add(str.substring(beginIndex, endIndex).trim());
beginIndex = endIndex;
endIndex += amt;
}
// Add the last string if any left
if(beginIndex < length) {
result.add(str.substring(beginIndex).trim());
}
return result;
}
public static void main(String[] args) {
String str = "hello world apple orange grapes juice spagehtti sauce milk";
descriptionFormatter(str, 34).forEach(System.out::println);
}
输出:
hello world apple orange grapes juice
spagehtti sauce milk