2013-02-26 30 views
0

我正在构建一个程序,通过扫描其书名页并使用OCR来获取本书的出版商...因为出版商总是处于标题页的底部,所以我认为由空间分隔的检测线是解决方案,但我不知道如何测试。这里是我的代码:如何检查字符串中的行是否由空格分隔?

Dim builder As New StringBuilder() 
Dim reader As New StringReader(txtOCR.Text) 
Dim iCounter As Integer = 0 
While True 
    Dim line As String = reader.ReadLine() 
    If line Is Nothing Then Exit While 

    'i want to put the condition here 

End While 
txtPublisher.Text = builder.ToString() 
+0

@Konrad鲁道夫感谢编辑我的问题我很抱歉,如果我不能解释清楚.. :) – user2107624 2013-02-26 10:00:43

回答

2

你的意思是空行吗?然后,你可以这样做:

Dim bEmpty As Boolean 

然后在循环中:

If line.Trim().Length = 0 Then 
    bEmpty = True 
Else 
    If bEmpty Then 
     '... 
    End If 

    bEmpty = False 
End If 
+0

感谢,但我的问题是后检测第二个空行如何将可能是发布者的下一行存储到文本框? – user2107624 2013-02-26 09:58:04

+0

@ user2107624您可以在每一行上循环,您可以使用布尔值来确定最后一行是否为空。我编辑我的答案。 – SysDragon 2013-02-26 09:59:46

+0

我想我现在知道了谢谢@SysDragon如果问问小老虎的问题,很抱歉,但非常感谢你的帮助:) – user2107624 2013-02-26 10:04:06

1

为什么不能做到以下几点:从底部走,直到你找到的第一个非空行(不知道OCR是如何工作的......也许最底部的行总是非空的,在这种情况下这是多余的)。在下一步中,直到第一个空行。中间的文字是出版商。

你不需要StringReader为:

Dim lines As String() = txtOCR.Text.Split(Environment.NewLine) 
Dim bottom As Integer = lines.Length - 1 

' Find bottom-most non-empty line. 
Do While String.IsNullOrWhitespace(lines(bottom)) 
    bottom -= 1 
Loop 

' Find empty line above that 
Dim top As Integer = bottom - 1 

Do Until String.IsNullOrWhitespace(lines(top)) 
    top -= 1 
Loop 

Dim publisherSubset As New String(bottom - top)() 
Array.Copy(lines, top + 1, publisherSubset, 0, bottom - top) 
Dim publisher As String = String.Join(Environment.NewLine, publisherSubset) 

但说实话,我不认为这是一个特别好的办法。它不灵活,不能很好地处理意外的格式。我会使用正则表达式来描述发布者字符串(及其上下文)的外观。也许这还不够,你必须考虑描述整个页面来推断哪些位是发布者。

+0

是的,这只是我的一个条件谢谢你的建议..我会尝试代码.. bottom-大多数行是一个空行,如何ocr工作我认为 – user2107624 2013-02-26 10:14:00

1

假设发布者总是在最后一行,并且总是出现在空行之后。那么下面可能是?

Dim Lines as New List(Of String) 
    Dim currentLine as String = "" 
    Dim previousLine as String = "" 

    Using reader As StreamReader = New StreamReader(txtOCR.Txt) 
    currentLine = reader.ReadLine 
    If String.IsNullOrWhiteSpace(previousLine) then lines.Add(currentLine) 
    previousLine = currentLine 
    End Using 

    txtPublisher.Text = lines.LastOrDefault() 

要忽略,如果以前的行是空/空:

Dim Lines as New List(Of String) 
Using reader As StreamReader = New StreamReader(txtOCR.Txt) 
lines.Add(reader.ReadLine) 
End Using 

txtPublisher.Text = lines.LastOrDefault() 
+0

您的代码获取第一行不是最后一行.. – user2107624 2013-02-26 10:30:15

+0

它可能会返回第一行的唯一方法是如果没有其他行在txt文件前面有一个空白/空行预先。我编辑了我的答案,以包含以下情况:如果您想要txt的最后一行,而不管前面的行是什么。 – freschx 2013-02-26 11:00:01

相关问题