How to count tabs in a text file?

studio54

New member
Joined
Mar 7, 2006
Messages
4
Programming Experience
Beginner
Hi,
First of, this is a nice communtiy here. Lots of info for anyone interested in furthering their knowledge of .NET . I am a career SQA who is looking to enhance his skills by learning a spot of programming, specifically .NET .

On this particular project that I'm working on; I am trying to parse through a text file line by line and counting the number of tabs in each line.

So far this is what I've managed to piece together.

Use a FileStream object to interact with the text file (open/close)
Then use a StreamReader object to read the file line by line using the ReadLine method.
I was thinking of putting the contents of each line as they are read one by one in a hidden ListBox on the form.
Then I can work with each line as I need and when done clear the listBox and move on to the next line in the file.

Assuming that this is reasonably sound so far, how do I count the number of tabs in each line?

I'm not looking for someone to write code for me, I just want to be pointed in the right direction.

If I'm headed in the wrong direction I would appreciate it if you would let me know what it is I'm doing wrong.

Thanx much.

Cheers.
 
Last edited:
Regular expressions is ideal for this type string operation, the expression "\t+" will get all occurences of the tab character. Here is some code, the inputstring can be only one line if you want to count line by line, or a string containing the whole filecontents.
VB.NET:
Dim inputstring As String = "hello" & vbTab & "tabs" & vbTab
Dim tabcount As Integer = System.Text.RegularExpressions.Regex.Matches(inputstring, "\t+").Count
MsgBox(tabcount.ToString)
 
Thank You! :D
 
If I may ask, what would one look this type of information under?
I have the .Net Architect suite installed, with all the help files and tutorials, etc.
If you were searching for an answer to this problem, what would you have queried in the help search box?
 
I deleted your other thread as requested. You could have entered the post in edit mode to get option to delete.

Regular Expressions is logically placed in the System.Text namespace, and could also be found searching for 'strings', but I guess it was easier if you ever heard about such a thing as Regular Expressions before. A good site if you found the topic interesting: http://www.regular-expressions.info/
 
Thanks for deleting the thread and for the regular expressions site.

I didn't want to cross thread as it is in my experience (doesn't matter what type of forum) something that is extremely 'frowned' upon.

Also, how do u delete a thread?
I clicked the Edit button and got these choices:-
"Save" - "Go Advanced" - "Cancel".

If I click the "Go Advanced" I get to either "Save Changes" or "Preview Changes".

No delete option that I could find, unless I'm looking in the wrong area of the screen....
 
another option for finding tabs in a string would be to use the .indexof(inString, controlchars.tab) which will find the first tab position, then if found iteratively repeat the .index of call starting at that position +1 and count the total found. here's the reference for the .indexof method:

The benefit to this approach would be speed if your concerned about that. An .indexof operation can complete on a pretty big string of text in about .01 seconds... if you're processing a 5GB file or something, that can add up. The RegEx approach from JohnH is nice and easier and if you're not processing huge files would be the way to go, but I just wanted to share another option as the RegEx is significantly slower because of the objects/arrays it creates with the matches, etc.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemstringclassindexoftopic.asp
 
5GB textfile is really something! There is some overhead to regex, but it is also designed to handle quite complex operations without additional coding. I did a simple test with a 36MB text file and compared measures of regex and normal string methods. Both tests took just less than one second to process and regex added only 93ms. So for regular textbased files usually below MB speed is not an issue.

Btw, looks like I got a bit greedy with the suggested regex.. "\t{1}" would be the one asked for. The "\t+" will count double tabs as a match and move on.

Also, studio54, appears 'delete' option only displays when it is possible to delete.
 
Back
Top