Vb.net 从字符串中提取2个单词
我正在处理收据布局,并试图将products descriptiontext分为两行,如果其长度超过24个字符 我的第一个解决方案是这样的:Vb.net 从字符串中提取2个单词,vb.net,substring,Vb.net,Substring,我正在处理收据布局,并试图将products descriptiontext分为两行,如果其长度超过24个字符 我的第一个解决方案是这样的: If Row.Description.Length >= 24 Then TextToPrint &= Row.Description.Substring(0, 24) & " $100" TextToPrint &= Row.Description.Substring(24) & vbNewLine els
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
希望我说得清楚/:如果大于24,那么从第23点开始寻找一个空格字符。找到后,在该位置拆分字符串。不过,您使用的“列”系统看起来相当糟糕-屏幕上的输出将流向何处?类似的功能应该可以工作:
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
我的第一枪
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;
}
私有静态列表拆分语句(字符串语句,整数计数)
{
如果(句子长度)到收据打印机。纸张上的一行有42个字符宽。我需要剩余的18个字符作为“金额”列的“右对齐”
string sentence = "A product with a long name that doesn't fit";
List<string> sentences = SplitSentence(sentence, 24);
sentences[0] = sentences[0] + " $100";