even slightly; you must instantiate a new object; which would be the case if we used the = and
± operators。 The StringBuilder type is like String; except the referenced text can be modified。
In the Process() implementation; the Do While loop calls the method Peek(); which reads;
but does not remove; a character value from the stream。 If there is nothing more to read; a …1
value is returned。 Otherwise; data is available; and the method ReadLine() can be called。
ReadLine() will read a buffer of characters until a newline or return character is encountered。
Having read a line of text; it is split into the individual fields using the Split() method。 The split
characters are the space and tab character (ControlChars。Tab)。
When the Split() method returns; the individual fields are assigned to the array splitUpText。
Those array elements are iterated and appended to the StringBuilder variable retval; but each
element is surrounded by a set of brackets。 The brackets provide a set of boundaries that you
can inspect to see what data has been found。 I include the brackets purely for debugging purposes。
Because I am trying to reformat the stream; I append a newline character (ControlChars。NewLine) to
the variable retval。
When all of the lines of text and fields within the lines of text are iterated; a string represen
tation of the StringBuilder instance is returned using the ToString() method。 Running the
code shows how many fields each line of text has and how you should format the text file。 This
gives you an understanding of how the file is structured。
The following is sample output from the lotto。txt file。
…………………………………………………………Page 287……………………………………………………………
CH A PT E R 1 0 ■ L E A R N I N G A B O U T P E R S IS T E N CE 265
(2000。01。15)(6)(10)(25)(26)(38)(42)(20)
(2000。01。19)(2)(16)(18)(23)(32)(43)(26)
(2000。01。22)(4)(5)(6)(24)(34)(38)(9)
(2000。01。26)(3)(20)(22)(24)(34)(39)(9)
(2000。01。29)(7)(12)(13)(34)(38)(39)(28)
(2000。02。02)(1)(18)(22)(28)(35)(43)(32)
(2000。02。05)(4)(13)(15)(31)(32)(45)(37)
(2000。02。09)(1)(29)(31)(34)(39)(41)(25)
。。。
(2006…12…27)(11)(13)(17)(21)(24)(26)(38)(578199)(735993)()()
(2006…12…30)(3)(13)(22)(30)(35)(41)(34)(142968)(472679)()
()
()
()
(2007…01…03)(5)(24)(37)(39)(41)(44)(9)(049802)(133875)()()
(2007…01…06)(3)(7)(23)(27)(30)(32)(38)(687442)(874814)()()
(2007…01…10)(7)(9)(13)(23)(35)(37)(25)(039498)(648301)()()
(2007…01…13)(3)(17)(22)(37)(39)(43)(34)(968842)(162860)()()
(2007…01…17)(12)(16)(27)(33)(37)(41)(24)(663824)(765917)()()
The sample output shows that we have the following items to fix:
o There are empty lines of text where no data has been defined。
o Some lines of text have empty fields at the end。
o Some fields have an incorrect date format。
o Some dates have duplicates; which need to be removed。
o Some lines of text have too many fields。 We need to figure out which fields we want to
keep and which we can discard。
■Note When processing streams and cleaning them up; it is important to take the stream apart first and
see what you are up against。 Do not make assumptions until you have looked at the individual pieces of data。
Then you will be able to determine the steps you need to undertake to fix the stream。
Fixing the Stream
The final solution uses the same code used to parse the lines of text and individual fields; as
follows (note; however; that we need the individual lines if the date format is correct; so we
store each one in the lineOfText variable):
…………………………………………………………Page 288……………………………………………………………
266 CH AP T E R 1 0 ■ L E A R N I N G A B OU T P E R S IS TE N CE
Public Class LottoTicketProcessor : Implements IProcessor
Private _dates As IList(Of String) = New List(Of String)()
。 。 。
Public Function Process(ByVal input As String) As String
Implements IProcessor。Process
Dim reader As TextReader = New StringReader(input)
Dim retval As StringBuilder = New StringBuilder()
Do While reader。Peek() …1
Dim lineOfText As String = reader。ReadLine()
Dim splitUpText As String() =
lineOfText。Split(New Char() {〃 〃c; ControlChars。Tab})
If _dates。Contains(splitUpText(0)) Then
Continue Do
End If
If splitUpText(0)。Length = 0 Then
Continue Do
End If
If splitUpText(0)。Contains(〃…〃) Then
Dim dateSplit As String() = splitUpText(0)。Split(New Char() {〃…〃c})
Dim newDate As String =
dateSplit(0) & 〃。〃 & dateSplit(1) & 〃。〃 & dateSplit(2)
If _dates。Contains(newDate) Then
Continue Do
End If
_dates。Add(newDate)
retval。Append(newDate)
For c1 As Integer = 0 To 7
retval。Append(〃 〃 & splitUpText(c1))
Next
Else
_dates。Add(splitUpText(0))
retval。Append(lineOfText)
End If
retval。Append(ControlChars。NewLine)
Loop
Return retval。ToString()
End Func
小说推荐
- oracle从入门到精通(PDF格式)
- -Page 1-Oracle 从入门到精通-Page 2-资源来自网络,仅供学习 Oracle 从入门到精通一、SQL 8
- 最新章:第37章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 最新章:第4章
- Java编程思想第4版[中文版](PDF格式)
- -Page 1-Page 2《Thinking In Java》中文版作者:Bruce Eckel主页:http/BruceEckel.编译:Trans Bot主页:http/memberease~transbot致谢-献给那些直到现在仍在孜孜不倦创造下一代计算机语言的人们!指导您利用万维网的语言进
- 最新章:第295章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 最新章:第309章
- VC语言6.0程序设计从入门到精通
- -Page 1-Visual C 6.0 程序设计从入门到精通求是科技 王正军 编著
- 最新章:第136章
- SQL 21日自学通(V3.0)(PDF格式)
- -Page 1-SQL 21 日自学通(V1.0 翻译人 笨猪目录目录 1译者的话 14第一周概貌 16从这里开始 16
- 最新章:第170章
- 2008年青年文摘精编版
- 作者:中国青年出版社“初恋”的惩罚.作者:凡 凡 文章来源《真情》2005年第4期 点击数:6608 更新时间:2005-6-5过了年,我就十八岁了。离高考只剩下四个多月了。这一段,班里的男女生相互间递纸条、写情书、约会等地下活动慢慢的多了起来。我这个“尖子生”也突然感到了不安、慌乱,并且自责。不知
- 最新章:第230章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 最新章:第28章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 最新章:第27章