我在C#中有一个代码,并且必须打印一个带有卖家名称的标签,但是我遇到了问题。 标签中的每一行符合20个字母,我有2行来放这个名字。在C中操纵字符串#
我需要在2行中安排卖方的名称,不要使用切词。
例如 - 名称:JOSE MAURICIO BERTOLOTO门德斯
线路1:JOSE MAURICIO
线路2:BERTOLOTO门德斯
有人知道我是怎样做到这一点? 感谢
编辑:基于的答案,我implemente此代码:
string[] SellerPrint = Seller.Split(' ');
Line1 = "";
Line2 = "";
foreach (string name in SellerPrint)
{
if (Line1.Length <= 20)
{
if ((Line1 + name).Length <= 20)
Line1 += (Line1.Length == 0) ? name : " " + name;
else
break;
}
}
Line2 = (Seller.Replace(Line1, "").Length <= 20) ? Seller.Replace(Line1+ " ", "") : Seller.Replace(Line1+ " ", "").Remove(20);
感谢您的帮助!
如果第二行会超过20个字符会怎么样? – 2012-03-30 20:05:19
左或右对齐:) – 2012-03-30 20:06:18
詹姆斯:随着第二行将是> 20个字符,我不得不削减额外的信件。 – JohnyMoraes 2012-03-30 20:08:25