在C#中操作字符串

Joh*_*aes -1 c# string

我在C#中有一个代码,并且必须打印一个带有卖家名字的标签,但我有一个问题.标签中的每一行都包含20个字母,我有2行代表这个名字.

我需要在2行中安排卖家的名字,不要切词.

例如 - 姓名:JOSE MAURICIO BERTOLOTO MENDES

Line1:JOSE MAURICIO

Line2:BERTOLOTO MENDES

有人知道我是怎么做到的吗?谢谢


编辑:根据答案,我实现了这段代码:

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);
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助!

Con*_*rix 5

您可以简单地将字符串拆分为单词string.Split(),然后添加到每个单词中,只要它足够小就可以添加到行中.

我也不会使用字符数,而是使用Graphics.MeasureString().