-
Jan 3rd, 2014, 03:00 PM
#1
Thread Starter
Lively Member
[RESOLVED] Counting special characters in a string?
Hi!
I am trying to create a code that counts characters in textbox(including whitespaces) and checks if it contains specific characters: |^€{}[]~
All these symbols mentioned above will increase the count by 2. Every other symbol/character will increase the count by 1.
I think my code I created is too...amateur. Is there another way to do this ?
This is the code I have so far:
Code:
' Symbols that take up 2 character space: |^€{}[]~
Dim c As Integer
Dim result As Integer = 0 'The number of counted characters
For c = 1 To Len(TextBox1.Text)
If Mid(TextBox1.Text, c, Len("€")) = "€" Then
result += 2
ElseIf Mid(TextBox1.Text, c, Len("|")) = "|" Then
result += 2
ElseIf Mid(TextBox1.Text, c, Len("^")) = "^" Then
result += 2
ElseIf Mid(TextBox1.Text, c, Len("{")) = "{" Then
result += 2
ElseIf Mid(TextBox1.Text, c, Len("}")) = "}" Then
result += 2
ElseIf Mid(TextBox1.Text, c, Len("[")) = "[" Then
result += 2
ElseIf Mid(TextBox1.Text, c, Len("]")) = "]" Then
result += 2
ElseIf Mid(TextBox1.Text, c, Len("~")) = "~" Then
result += 2
Else
result += 1
End If
Next
Me.Label1.Text = result
Thanks in advance!
-
Jan 3rd, 2014, 03:44 PM
#2
Re: Counting special characters in a string?
Your code is riddled with vb6 hold overs. Mid, Len... You are using VB6(i hope) so use NET methods.
vb Code:
Imports System.Text.RegularExpressions Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim matches = New Regex("[|\^€\{\}\[\]~]").Matches(Me.TextBox1.Text).Count Dim twoCountChars As Integer = matches * 2 Dim oneCountChars As Integer = Me.TextBox1.Text.Length - matches MessageBox.Show(String.Format("Total: {0}{1}Two count: {2}{1}One count: {3}{1}", twoCountChars + oneCountChars, Environment.NewLine, twoCountChars, oneCountChars)) End Sub End Class
-
Jan 3rd, 2014, 03:46 PM
#3
Re: Counting special characters in a string?
There is a lot of legacy coding going on there. Try doing something like this:
Code:
Dim specials() As String = {"|", "^", "€", "{", "}", "[", "]", "~"}
Dim count As Integer = 0
For i As Integer = 0 To TextBox1.TextLength - 1
Dim found As Boolean = False
For Each c As String In specials
If TextBox1.Text.Substring(i, 1).Contains(c) Then
found = True
Exit For
End If
Next
If found Then
count += 2
Else
count += 1
End If
Next
MessageBox.Show(count.ToString)
-
Jan 4th, 2014, 05:38 AM
#4
Thread Starter
Lively Member
Re: Counting special characters in a string?
Thank you ident and dday9! Wonderfully working codes.
One question more: how can I test code so that I know how much resource it is taking. Is there any tool for this built in Visual Studio?
Thank you all again, very much appreciated!
-
Jan 4th, 2014, 09:16 AM
#5
Re: Counting special characters in a string?
Is there any tool for this built in Visual Studio?
No, but you can get a rough guide using Task Manager which allows you to track a specific process. In debug mode you're looking for vshost.exe which will of course always be using rather more resources than the program left to its own devices. Having said that, unless you've got 20000 lines of code involving vast data storage and a massive memory leak it's extremely unlikely that your program will represent anything more than a tiny burden on the system.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Jan 4th, 2014, 09:28 AM
#6
Thread Starter
Lively Member
Re: Counting special characters in a string?
Thank you dunfiddlin!
Thread solved!
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
|