假设我们有一个长字符串,我们希望将字符串拆分为64个字符长而不分割单个字词:
We prefer questions that can be answered, not just discussed. If your question is about this website, ask it on meta instead.
如果我们这样分开:
string.SplitByLength(64).ToList();
我们最终会得到两个字符串:
We prefer questions that can be answered, not just discussed. I
f your question is about this website, ask it on meta instead.
分割此字符串的最优雅方法是什么,以便第一个字符串在If
之前结束,而秒字符串以If
开头?
换句话说,如何将长字符串拆分成等于或短于所需长度的字符串列表,同时不拆分任何单个字,而是拆分字之间最后可能的空白空间?
答案 0 :(得分:6)
你可以给它一个像64这样的最大值,然后用它作为索引向后搜索并找到第一个空格并将其拆分。使用递归对剩余的字符串重复,然后就完成了。
public static IEnumerable<string> SmartSplit(this string input, int maxLength)
{
int i = 0;
while(i + maxLength < input.Length)
{
int index = input.LastIndexOf(' ', i + maxLength);
if(index<=0) //if word length > maxLength.
{
index=maxLength;
}
yield return input.Substring(i, index - i);
i = index + 1;
}
yield return input.Substring(i);
}
答案 1 :(得分:3)
var phrase = "We prefer questions that can be answered, not just discussed. If your question is about this website, ask it on meta instead. We prefer questions that can be answered, not just discussed. If your question is about this website, ask it on meta instead.";
var regex = new Regex(@"(.{1,64})(?:\s|$)");
var results = regex.Matches(phrase)
.Cast<Match>()
.Select(m => m.Groups[1].Value)
.ToList();
编辑:我明白了......
答案 2 :(得分:0)
我不太确定它是否是最优雅的,但为什么不在空格上分割字符串然后将一个整数作为计数器,如果它低于64,则为stringbuilder添加单词和空格,如果是高于64然后你将当前的stringbuilder添加到列表,创建新的stringbuilder并重置计数器?