如何在VB.NET中进行证明 [英] How to Justify in VB.NET

查看:68
本文介绍了如何在VB.NET中进行证明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有这种情况,我将一个句子声明为字符串.如果第一行达到极限,我将要在下一行中写.但是我的问题是,如果长度的结尾部分是一个单词,那么应该在忽略最大长度的下一行中写出来.

这是示例

dim str as string =这是我的工作示例.

-我的第一行最多只能写12
obj.writeline(len(str,12)&vbNewLine)输出---->这是前任
obj.writeline(str.substring(12))output -------->我的工作很充实.

但是我想要的就是这样.

这是
我工作的例子.

非常感谢您的帮助.

Hi,

I have this scenario which I declare a sentence into string. what I need to do if the first line reach the limit it will write in the next line. But my problem what if the end part of the length is a word this should be write in the next line neglecting the max length.

Here is the example

dim str as string = This is an example of my work.

--my first line should write only max of 12
obj.writeline(len(str,12) & vbNewLine) output----> This is an ex
obj.writeline(str.substring(12))output --------> ample of my work.

But What I want is to be like this.

This is an
example of my work.

Your help much appreciated.

推荐答案

您好,要获得结果,您需要从第一行的最大位置开始查找第一个空格字符,然后它将是第一行的结尾,而字符串的其余部分将成为第二行.

Hi, to achieve the result you want you need to work back from the maximum position for the first line to find the first space character, which would then be the end of the first line and the remainder of the string will become the second line.

Dim str As String = "This is an example of my work."
Dim splitPos As Integer = 12
While (str(splitPos) <> " ")
    splitPos = splitPos - 1
End While
Dim line1 As String = str.Substring(0, splitPos).Trim()
Dim line2 As String = str.Substring(splitPos).Trim()
Console.WriteLine(line1)
Console.WriteLine(line2)



输出:
这是
我的工作示例.


注意:如果字符串少于12个字符或在前12个字符中没有空格,则此代码无法处理问题.

如果您要写一个长句子并希望将其拆分为多行,可以使用另一种方法将句子拆分为单词,然后构建一行,直到达到行数限制并输出该行,然后继续构建下一行.



Output:
This is an
example of my work.


Note: This code doesn''t handle issues if the string is less than 12 characters or if there are no spaces found in the first 12 characters.

Another method you could use if you are writing a long sentence and want it split into multiple lines would be split the sentence into words and then build up a line until you reach the line limit and the output that line, then continue building the next line.

Dim str As String = "This is an example of my work."
Dim words As String() = str.Split(New Char() {" "})
Dim line As String = ""
Dim maxLen As Integer = 12
For Each word In words
    '' Is the length of the current line plus a space character and the next work too long for this line?
    If (line.Length + 1 + word.Length > maxLen) Then
        '' Yes, write out the current line, and reset
        Console.WriteLine(line)
        line = ""
    End If
    '' If this is not the first word add a space character between words
    If line.Length > 0 Then
        line = line + " "
    End If
    line = line + word
Next
Console.WriteLine(line)



输出:
这是

的示例 我的工作.


注意:如果要更改每行的长度,可以在写入每行之后更改maxLen变量,但是您将需要添加行计数器.

希望这会有所帮助.



Output:
This is an
example of
my work.


Note: If you want to vary the length of each line you can change the maxLen variable after writing each line, but you will need to add a line counter.

Hope this helps.


这篇关于如何在VB.NET中进行证明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆