读取单词"AREA CODE:";从我的文本文件 [英] Reading a word "AREA CODE :" from my text file
问题描述
我有一个文本文件,如下所示:
I have a text file as below:
BRANCH :SARDAR BHILADWALA PARDI PEOPLE'S CO-OP. ,Head Office NODE : 9
BRANCH CODE : 1 RUN DATE :11/05/2009 USER : SNA
REPORT ID :R048260 * AREA WISE MEMBER LISTING OF SHARE CUSTOMERS ** PAGE : 1
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
Selection :- Product Code : SHM SHARE CAPITAL
All/Selected/Range : A ALL
Specific Area Code :
From Area Code :
To Area Code :
All/Selected/Range : A ALL
Specific Sub Area Code :
From Sub Area Code :
To Sub Area Code : 9999 **********
Skip Closed Accts : Y YES
As On Date : 31/03/2009
------------------------------------------|
| Area Code : 1 PARDI |
------------------------------------------|
| Sub Area Code : |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Sub Sub Area Code: |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Sr.No | Membership No | Name | Addresss | Occupation |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
| | 35957 | SHAH HEMANGINI KANAIYALAL NATVARLAL |SHROFF STREET,, KILLA PARDI, DIST.VALSAD, Pardi, Pin Cd : 396125 |
| 1 | 35959 | SARSIWALA SAKINABEN ASGARALI ABDULHUSEIN |OPP.HIGHPOWER SODA FACTORY,, DUNGRI FALIA,DAMNI ZAPA,, KILLA PARDI.DIST.VAL |
| 2 | 36022 | PATEL BHARTIBEN BABUBHAI KHANDUBHAI |B/H BALMANDIR,, KANSARWAD,, KILLA PARDI., Pardi, Pin Cd : 396125 |
| 3 | 36023 | PATEL ASHISHKUMAR BABUBHAI KHANDUBHAI |B/H BALMANDIR,, KANSARWAD,, KILLA PARDI., Pardi, Pin Cd : 396125 |
| 4 | 36025 | SHAIKH HALIMABIBI UMARBHAI MOHMEDBHAI |OPP.JALARAM KHAMAN HOUSE,, CHIVAL ROAD,, KILLA PARDI.DIST.VALSAD, Pardi, Pi |
| 5 | 36072 | LAD PUSHPABEN VINODBHAI BHANABHAI |KUMBHARWAD,, VALSADI ZAPA,, KILLA PARDI,DIST.VALSAD, Pardi, Pin Cd : 396125 |
| 7 | 36076 | KOTHARI PANNABEN JANAKBHAI ISHVARLAL |VANIAWAD,, KILLA PARDI, DIST.VALSAD, Pardi, Pin Cd : 396125 |
| 8 | 36080 | PATEL DHARMESH BHAGUBHAI KANJIBHAI |NEAR RATAN RICE MILL,, DAMNI ZAPA,, KILLA PARDI., Pardi, Pin Cd : 396125 |
| 9 | 36081 | MAPARA BANKIMKUMAR SUNDARLAL HARJIVANDAS |BALAKHADI,, STATION ROAD,,
从该文本文件中,我只想读取"AREA CODE:"一词,然后需要将"AREA CODE:"之后的文本存储在变量中.
我也可以读取文件,但我的问题是我正在获取包含"AREA CODE:"的所有行.相反,我只需要文本文件中的"AREA CODE:1 PARDI" ...
我当前的代码
[OP注释中添加的代码] Milind
From this text file I want to read only "AREA CODE :" word and then the text after "AREA CODE :" needs to be stored in a variable.
I am able to read file also find the word but my problem is I am getting all lines which contain "AREA CODE:".Instead I need only "AREA CODE : 1 PARDI" from text file...
My current code
[Added code from OPs comment] Milind
objStreamReader = New StreamReader("E:\Rachna\ELECTION 2009-SHAREHOLDERS AREAWISE.txt")
strLine = objStreamReader.ReadLine
Do While Not strLine Is Nothing
'MsgBox(strLine)
If strLine.ToUpper.Contains("Area Code :".ToUpper) Then
strLine.Trim()
strAreaCode = Split(strLine, " ")
End If
'Dim var As String = strLine.StartsWith("Area Code :")
strLine = objStreamReader.ReadLine
Loop
objStreamReader.Close()
这是读取文件并找到"AREA CODE:"字的代码.但是它会从我不需要的txtfile中重新调整"From Area Code:"行.
This is code where File is read and "AREA CODE :" word is found. But it retuns line "From Area Code :" from txtfile which I dont need.
Can any1 please help me?
推荐答案
Dim StrAreaCode As New List(Of String) 'collect area-codes
Dim objStreamReader As New IO.StreamReader("E:\a.txt")
Dim strLine = objStreamReader.ReadLine
'Declare table add columns
Dim ReadGridFlag = -1
Do While Not strLine Is Nothing
If strLine.Trim.ToUpper.StartsWith("Area Code :".ToUpper) Then
strLine.Trim()
StrAreaCode.Add(strLine.Split(" :")(1)) 'ADD AreaCode in list
End If
strLine = objStreamReader.ReadLine
If strLine.Trim.ToUpper.StartsWith("| Sr.No".ToUpper) Then
ReadGridFlag = 0
ElseIf ReadGridFlag = 1 Then
'split text with "|" and
'add row in table
'now assign splitted values to respective cell in table
ElseIf strLine.Trim.ToUpper.StartsWith("|--".ToUpper) And ReadGridFlag = 0 Then
ReadGridFlag = 1
End If
Loop
objStreamReader.Close()
祝您编码愉快!
:)
Happy Coding!
:)
我只需要替换一行代码,它就可以正常工作.
写
I would simply replace one line of your code and it should work fine.
Write
If strLine.ToUpper.Contains("| Area Code :".ToUpper) Then
的界面
intead of
If strLine.ToUpper.Contains("Area Code :".ToUpper) Then
而且你应该没事.
Milind
And you should be fine.
Milind
这里是一个示例方法.
将文件的内容读取为字符串,然后使用 split [
Here is a sample approach.
Read the contents of the file into a string and then use the split [^] method to separate each line of the file.
Then loop through each line of the file and check if that line starts with the word "Area Code" to retrieve the area code. Here is the code sample
Dim AreaCode As String = ""
Using sr As New StreamReader("D:\Test\Test.txt")
Dim strFileData As String = sr.ReadToEnd()
Dim strInputLines As String() = strFileData.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
For Each Data As String In strInputLines
If Data.Trim().ToUpper().StartsWith("AREA CODE") Then
Dim AreaCodeLine As String() = Data.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
If AreaCodeLine.Length > 1 Then
AreaCode = AreaCodeLine(1)
End If
End If
Next
End Using
PS:我同意Milind的观点.您应该在问题上更具体地说明到目前为止已尝试了哪些操作,并在尝试过后再发布代码.
PS: I agree with Milind. You should be more specific in your question on what have you tried so far and also post your code when you have tried something.
这篇关于读取单词"AREA CODE:";从我的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!