Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2010 Counting special characters in a string?

$
0
0
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! ;)

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>