Results 1 to 6 of 6

Thread: [RESOLVED] Counting special characters in a string?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    Resolved [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!

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    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:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object,
    6.                               ByVal e As System.EventArgs) Handles Button1.Click
    7.         Dim matches = New Regex("[|\^€\{\}\[\]~]").Matches(Me.TextBox1.Text).Count
    8.         Dim twoCountChars As Integer = matches * 2
    9.         Dim oneCountChars As Integer = Me.TextBox1.Text.Length - matches
    10.  
    11.         MessageBox.Show(String.Format("Total: {0}{1}Two count: {2}{1}One count: {3}{1}",
    12.                                        twoCountChars + oneCountChars,
    13.                                        Environment.NewLine,
    14.                                        twoCountChars,
    15.                                        oneCountChars))
    16.     End Sub
    17. End Class

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    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)
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    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!

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2012
    Posts
    75

    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
  •  



Click Here to Expand Forum to Full Width