|
-
Nov 18th, 2011, 02:00 PM
#1
Thread Starter
New Member
Character Counting In a Textfile
How do i count the number of visible characters in a text file in vb 2008 (ignoring spaces, tabs, carriage returns, spaces etc) ?
I need to display this amount in a label.
Any help is appreciated
thanks
-
Nov 18th, 2011, 02:30 PM
#2
Re: Character Counting In a Textfile
Suggestion: Load it in a textbox and do the count via .Lenght. Or am I thinking to simple?
-
Nov 18th, 2011, 02:48 PM
#3
Thread Starter
New Member
Re: Character Counting In a Textfile
I'm not sure could you provide sample code?
for returning only visible characters. Because when i do it it returns carriage returns, spaces and tabs with the character count
-
Nov 18th, 2011, 02:49 PM
#4
Re: Character Counting In a Textfile
Try this. Somebody else might have a better solution though.
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim VisibleCharacters As Integer = 0
Dim SingleCharacter As Char() = File.ReadAllText("C:\Test.txt").ToCharArray
For i As Integer = 0 To SingleCharacter.Length - 1
Select Case SingleCharacter(i)
Case ControlChars.Tab
Case ControlChars.Lf
Case ControlChars.Cr
Case " "
Case Else
VisibleCharacters += 1
End Select
Next i
Label1.Text = String.Format("Total: {0} characters ({1} characters are visible).", SingleCharacter.Length, VisibleCharacters)
End Sub
If you don't want to use an array, like you said in the PM you've sent me, then you can use Substring.
vb.net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim VisibleCharacters As Integer = 0
Dim AllText As String = File.ReadAllText("C:\Text.txt")
For i As Integer = 0 To AllText.Length - 1
Select Case AllText.Substring(i, 1)
Case ControlChars.Tab
Case ControlChars.Lf
Case ControlChars.Cr
Case " "
Case Else
VisibleCharacters += 1
End Select
Next
Label1.Text = String.Format("Total: {0} characters ({1} characters are visible).", AllText.Length, VisibleCharacters)
End Sub
Last edited by Chris001; Nov 18th, 2011 at 03:37 PM.
Reason: typo
-
Nov 18th, 2011, 03:46 PM
#5
Re: Character Counting In a Textfile
If you are okay with a little LINQ the following reports back the character count minus characters in the array UnWantedValues then reports back the total including UnWantedValues array characters. Lastly a language extension method provides a line count of the file.
To use simple change the first line to a file you want to check on.
Code:
Module Module2
Public Sub Demo()
Dim Lines = IO.File.ReadAllText(IO.Path.Combine(Application.StartupPath, "Document.txt"))
Dim CharGroup = _
( _
From c In Lines.ToCharArray() _
Group c By c Into Group _
Select New With {.Item = c, .Occurrences = Group.Count, .Numb = Asc(c)}).ToList _
.OrderBy(Function(x) x.Item)
Dim UnWantedValues As Integer() = {9, 10, 13, 32}
Dim Total = (From Item In CharGroup _
Where Not UnWantedValues.Contains(Item.Numb) _
Select Item.Occurrences).Sum
Dim Total1 = (From Item In CharGroup Select Item.Occurrences).Sum
Console.WriteLine("Total less unwanted {0} Total {1}", Total, Total1)
Console.WriteLine("Line count: {0}", Lines.LineCount)
End Sub
<System.Diagnostics.DebuggerStepThrough()> _
<Runtime.CompilerServices.Extension()> _
Public Function LineCount(ByVal value As String) As Integer
Return System.Text.RegularExpressions.Regex.Matches(value, ".+\n*").Count
End Function
End Module
-
Nov 18th, 2011, 03:56 PM
#6
Re: Character Counting In a Textfile
Code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim inputString = String.Format("Peter Piper {0} picked a peck of {1} pickled peppers", ControlChars.NewLine, ControlChars.Tab)
Dim visibleCount = CountVisibleCharacters(inputString)
End Sub
Private Function CountVisibleCharacters(s As String) As Integer
Return System.Text.RegularExpressions.Regex.Matches(s, "[\x21-\x7E]").Count
End Function
http://www.regular-expressions.info/posixbrackets.html
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|