0

我正在使用收据布局并尝试将产品描述文本分成 2 行,如果其长度超过 24 个字符。

我的第一个解决方案是这样的:

If Row.Description.Length >= 24 Then
TextToPrint &= Row.Description.Substring(0, 24) & "      $100"
TextToPrint &= Row.Description.Substring(24) & vbNewLine
else
 TextToPrint &= Row.Description & filloutFunction(Row.Description.length) &"      $100" & vbNewLine
end if

但这给出了这个结果。

A product with a long na   $100
me that doesn't fit        

我不知道如何制作一个函数来划分描述,使其看起来像我们通常看到的那样。

A product with a long      $100
name that doesn't fit   

希望我说清楚/:

4

3 回答 3

1

如果大于 24,则从第 23 点开始递减查找空格字符。找到它后,在该位置拆分字符串。不过,您拥有的那个“列”系统看起来很讨厌-屏幕上的这个输出到哪里去了?

于 2009-09-29T12:13:32.953 回答
0

像这样的东西应该工作:

Dim yourString = "This is a pretty ugly test which should be long enough"
Dim inserted As Boolean = False
For pos As Integer = 24 To 0 Step -1
    If Not inserted AndAlso yourString(pos) = " "c Then
        yourString = yourString.Substring(0, pos + 1) & Environment.NewLine & yourString.Substring(pos + 1)
        inserted = True
    End If
Next
于 2009-09-29T12:23:17.903 回答
0

我的第一枪,

private static List<string> SplitSentence(string sentence, int count)
{
    if(sentence.Length <= count)
    {
        return new List<string>()
               {
                   sentence
               };
    }

    string extract = sentence.Substring(0, sentence.Substring(0, count).LastIndexOfAny(new[]
                                                                                      {
                                                                                          ' '
                                                                                      }));

    List<string> list = SplitSentence(sentence.Remove(0, extract.Length), count);

    list.Insert(0, extract.Trim());

    return list;
}

所以 :

string sentence = "A product with a long name that doesn't fit";

List<string> sentences = SplitSentence(sentence, 24);
sentences[0] = sentences[0] + "      $100";

我认为可以优化。

于 2009-09-29T12:34:21.450 回答