Hello,
I am trying to get VB to count how many A's B's C's D's E's etc... that are in a declared string, can someone tell me how to do this.
thanks
Ray
RFeather[AT][NOSPAM]chstaffing.com
Hello,
I am trying to get VB to count how many A's B's C's D's E's etc... that are in a declared string, can someone tell me how to do this.
thanks
Ray
RFeather[AT][NOSPAM]chstaffing.com
Counting Letters in a string
EricA MSFT
Eeeeeeeeeeeeeeeeeeeeeewwwww
Ugly, ugly ugly....
Dim str As String = "a982734hfa98sudwhebwe8ruwbwe8r4b233"
Dim Others As Integer
Dim Alpha(25) As Integer< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
For i As Short = 0 To str.Length - 1
Try
Alpha((AscW(str.Substring((i)).ToLower) - 97)) += 1
Catch
Others += 1
End Try
Next
For i As Short = 0 To 25
TextBox1.Text &= vbCrLf & CStr(Chr(i + 65)) & "'s " & Alpha(i)
Next
TextBox1.Text &= vbCrLf & "Others: " & Others
A's 2
B's 3
C's 0
D's 1
E's 3
F's 1
G's 0
H's 2
I's 0
J's 0
K's 0
L's 0
M's 0
N's 0
O's 0
P's 0
Q's 0
R's 2
S's 1
T's 0
U's 2
V's 0
W's 4
X's 0
Y's 0
Z's 0
Others: 14
{Help militate for emoticons with hair.!!!!!!!!!}
WariaReiMasi
kolbe144
if you read my comments, I am not complaining about the capabilities. . . I am complaining about the language. Don't get me started on the lacking features of the IDE when programming VB.
compare the above code to this c# (a 15% reduction in the amount of actual code)
================================
public class CharacterCount
{
private string _value;
private Dictionary<char, int> _dictionary = new Dictionary<char, int>();
public event EventHandler StringChanged;
public CharacterCount(string s)
{
_Value = s;
this.Parse();
}
private void Parse()
{
_dictionary.Clear();
Char[] chrs = _value.ToCharArray();
foreach (char ch in chrs)
_dictionary(ch) = this[ch] + 1;
}
public int this[char c]
{
get
{
int n;
if (!_dictionary.TryGetValue(c, n)) n = 0;
return n;
}
}
public string InitialString
{
get{ return _value;}
set
{
_value = value;
this.Parse();
this.DoStringChanged(EventArgs.Empty);
}
}
public Dictionary<char, int> CountList
{
get{ return mDictionary;}
}
protected virtual void DoStringChanged(EventArgs e)
{
if (this.StringChanged != null)
this.StringChanged(this, e);
}
public List<string> AsStringList
{
get
{
List<string> result = new List<string>();
foreach(char ch in _dictionary.Keys)
result.Add(string.Format("{0}: {1}", ch, _Dictionary(ch)));
}
}
}
================================
SpikeBoy
This looks like alot of code, but thats because it is in VB. . .
download source code here
ok. . . create a new Windows Application
drop a ListBox on the main form
drop a TextBox and call it "InitValue"
drop another Text Box and Call it "TestChars"
drop a Button and call it "Count"
add to the project a Class module called "CharacterCounter"
add this code to the Module CharacterCounter:
=======================================
Imports System.Collections.Generic
Public Class CharacterCount
Private mValue As String
Private mDictionary As _
New Dictionary(Of Char, Integer)
Public Event StringChanged As EventHandler
Public Sub New(ByVal s As String)
mValue = s
Me.Parse()
End Sub
Private Sub Parse()
mDictionary.Clear()
Dim chrs() As Char = mValue.ToCharArray()
For Each ch As Char In chrs
mDictionary(ch) = Me(ch) + 1
Next
End Sub
Default Public ReadOnly Property Item(ByVal c As Char)
Get
Dim n As Integer
If Not mDictionary.TryGetValue(c, n) Then n = 0
Return n
End Get
End Property
Public Property InitialString() As String
Get
Return mValue
End Get
Set(ByVal value As String)
mValue = value
Parse()
DoStringChanged(EventArgs.Empty)
End Set
End Property
Public ReadOnly Property CountList() As _
Dictionary(Of Char, Integer)
Get
Return mDictionary
End Get
End Property
Protected Overridable Sub DoStringChanged _
(ByVal e As EventArgs)
RaiseEvent StringChanged(Me, e)
End Sub
Public ReadOnly Property AsStringList() As _
List(Of String)
Get
Dim result As New List(Of String)
For Each ch As Char In mDictionary.Keys
result.Add(String.Format("{0}: {1}", ch, mDictionary(ch)))
Next
Return result
End Get
End Property
End Class
=======================================
put this code in the Main Form:
=======================================
Public Class Form1
Private WithEvents mCounter As New CharacterCount("")
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
ListBox1.Items.Clear()
InitValue.Text = ""
TestChars.Text = ""
TestChars.MaxLength = 1
End Sub
Private Sub Value_TextChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles InitValue.TextChanged
mCounter.InitialString = InitValue.Text
End Sub
Private Sub mCounter_StringChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles mCounter.StringChanged
ListBox1.DataSource = mCounter.AsStringList
End Sub
Private Sub Count_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Count.Click
MessageBox.Show(Me, String.Format("{0}: {1}", _
TestChars.Text.Chars(0), _
mCounter(TestChars.Text.Chars(0))))
End Sub
Private Sub Test_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles TestChars.TextChanged
Count.Enabled = TestChars.Text.Length > 0
End Sub
End Class
=======================================
I don't get how people could put up with this awful language!!! Everytime I have to translate something that is so simple and direct in C# I am amazed at how convoluted it looks in vb!
"SAVE A TREE! DROP VB!!!"
Someone please tell my why they use the word "Overrideable" instead of "virtual"
I would be insulted if I were a Vb'er. "What MS thinks I won't understand OO terminology "
Hussain Noordin
For i = 0 To myString.Length- 1 Step i + 1
This line of code returns an invalid qualifier compile error. I am not sure why everything looks good. I like this snipet because I can modify it to also count the numbers.
Thanks
-Matthew-
Blair,
Mabye it looks convoluted to you because you're obviously not familiar enough with VB to understand it as well as other more seasoned developers.
And...why, tell me, is C# "far superior" to VB
They're actually almost identical in terms of their capabilities....in .NET 2005 that is.
Come on Blair, think before you speak.
Andrew L Arnott
Dim aCount As Integer = 0
Dim bCount As Integer = 0
Dim cCount As Integer = 0
Dim myString As String = "aabbccc"
Dim i As Integer
For i = 0 To myString.Lengt- 1 Step i + 1
Dim charAsLower As String = myString(i).ToString().ToLower()
Select Case charAsLower
Case "a"
aCount = aCount + 1
Exit For
Case "b"
bCount = bCount + 1
Exit For
Case "c"
cCount = cCount + 1
Exit For
End Select
Next
'----------------------------------------------------------------
' Converted from C# to VB .NET using CSharpToVBConverter(1.2).
' Developed by: Kamal Patel (http://www.KamalPatel.net)
'----------------------------------------------------------------
halebob
John SP
in the far superior c#:
public class CharCounter
{
public static Dictionary<char, int> DoCount(string str)
{
Dictionary<char, int> result = new Dictionary<char,int>();
char[] chrs = str.ToCharArray();
foreach (char c in chrs)
{
int temp;
if (!result.TryGetValue(c, out temp))
result.Add(c, 1);
else
result[ c ] = temp++;
}
return result;
}
public static void Test()
{
Dictionary<char, int> test = DoCount("Now is the time for all good men to come to the aid of their country");
foreach (char c in test.Keys)
{
Console.WriteLine(string.Format("'{0}' : {1} ", c.ToString(), test[ c ]));
}
}
}
HeroCat
...but not very efficient, unfortunately, since it'd be have to be called 26 times to find all the letters.
LOL!
dabagaalle
Jacob Dickinson
take a look at the dictionary example. . .
It gives you a structure that contains the letters and the count.
If you give me a moment I will do it in vb
biswebdotws
Just in case you need it. . . sorted list
add this function to CharacterCount:
==============
Private Function Sort() As Dictionary(Of Char, Int32)
Dim result As New Dictionary(Of Char, Integer)
Dim al As New List(Of Char)
al.AddRange(mDictionary.Keys)
al.Sort()
For Each ch As Char In al
result.Add(ch, mDictionary(ch))
Next
Return result
End Function
===============
and change Parse to this:
===============
Private Sub Parse()
mDictionary.Clear()
Dim chrs() As Char = mValue.ToCharArray()
For Each ch As Char In chrs
mDictionary(ch) = Me(ch) + 1
Next
mDictionary = Sort()
End Sub
================
You can extend for exclusions and case sensitivity, too.
t1
Nice simple function;
Public Function Tally(ByVal Expression As String, ByVal Find As String) As LongDim Compare As String = Expression.Replace(Find, vbNullString)
Return (Expression.Length - Compare.Length)
End Function