寻找最佳解决方案以创建多个子字符串,以使每个子字符串的长度不超过参数中的长度值。当包含在子字符串中时,它将空格转换为指定的分隔符(以逗号为例),并删除多余的空格。 示例:
input string = "Smoking is one of the leading causes of statistics"
length of substring = 7
result = "Smoking,is one,of the,leading,causes,of,statist,ics."
input string = "Let this be a reminder to you all that this organization will not tolerate failure."
length of substring = 30
result = "let this be a reminder to you,all that this organization_will not tolerate failure."
答案 0 :(得分:0)
我认为,最困难的部分是处理空间。这就是我想出的
class RoundRectLayer : CALayer {
var rect : CGRect = .zero
override func draw(in ctx: CGContext) {
ctx.saveGState()
var path = CGPath(roundedRect: rect,
cornerWidth: rect.size.width / 10,
cornerHeight: rect.size.height / 10,
transform: nil)
ctx.addPath(path)
ctx.setFillColor(UIColor.clear.cgColor)
ctx.fillPath()
path = path.copy(strokingWithWidth: 4.0,
lineCap: CGLineCap.butt,
lineJoin: CGLineJoin.bevel,
miterLimit: 0)
ctx.addPath(path)
let colors = [
UIColor.yellow.cgColor,
UIColor.red.cgColor
]
ctx.fillPath()
ctx.restoreGState()
}
}
然后您可以这样称呼
private string SplitString(string s, int maxLength)
{
string rest = s + " ";
List<string> parts = new List<string>();
while (rest != "")
{
var part = rest.Substring(0, Math.Min(maxLength, rest.Length));
var startOfNextString = Math.Min(maxLength, rest.Length);
var lastSpace = part.LastIndexOf(" ");
if (lastSpace != -1)
{
part = rest.Substring(0, lastSpace);
startOfNextString = lastSpace;
}
parts.Add(part);
rest = rest.Substring(startOfNextString).TrimStart(new Char[] {' '});
}
return String.Join(",", parts);
}
,输出为
Console.WriteLine(SplitString("Smoking is one of the leading causes of statistics", 7)); Console.WriteLine(SplitString("Let this be a reminder to you all that this organization will not tolerate failure.", 30));