Results 1 to 9 of 9

Thread: [RESOLVED] Count string

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    29

    Resolved [RESOLVED] Count string

    hi,

    Can anyone show how to count a string

    for example:

    dim a as string
    a = "abcdefghij"

    how can i count the string above
    the answer should be 10.

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Count string

    The String object is a class that has methods and properties. One of the properties of the String class is Length, which returns the length of the string.

    When you type "a." you should get a list of methods and properties that are publicly available from that object. A lot of the time, you can find things like this yourself by just looking through the names of the properties and methods.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    29

    Re: Count string

    thanks for the answer..

    but how can i eliminate the space between the string?

    dim a = "abc def ghij"

    the answer should be 10 also..

    i using trim function but it doesn't work

    any idea?

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Count string

    If you want to get rid of a space you can Replace a space with a blank string.

  5. #5
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: Count string

    Here's some code. It uses a textbox, label and button on a form.

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String
            Dim NumberOfCharacters, IndexOfSpace As Integer
            str = TextBox1.Text
            str = str.Trim  ' First remove leading and following spaces from string. 
            Do Until str.IndexOf(" ") = -1 ' If " " isn't in the string then str.IndexOf(" ") will give a value of -1
                IndexOfSpace = str.IndexOf(" ") ' Loop until there are no spaces in string
                str = str.Remove(IndexOfSpace, 1)
            Loop
            NumberOfCharacters = str.Length
            Label1.Text = "There are " & NumberOfCharacters & " characters in " & str & "."
        End Sub
    End Class
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

  6. #6
    New Member
    Join Date
    Oct 2009
    Posts
    1

    Re: Count string

    ' You may want to ignore other characters also:
    Public Class Form1
    ' Ignore spaces, newlines, and tabs
    Dim IgnoreChar As String = _
    ControlChars.Tab & ControlChars.Cr & ControlChars.Lf & " "

    Dim TestString As String = _
    "ab c" & ControlChars.Tab & "d" & ControlChars.NewLine & "e"

    Dim count As Integer = 0



    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    count = 0
    Dim CharacterToInspect As Char
    For StringPosition As Integer = 1 To TestString.Length
    CharacterToInspect = GetChar(TestString, StringPosition)
    If Not IgnoreChar.Contains(CharacterToInspect) Then
    count += 1
    End If
    Next

    MsgBox(count) ' Answer = 5

    End Sub
    End Class

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2009
    Posts
    29

    Re: Count string

    thnks guy

  8. #8
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: Count string

    Quote Originally Posted by EntityX View Post
    Here's some code. It uses a textbox, label and button on a form.

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String
            Dim NumberOfCharacters, IndexOfSpace As Integer
            str = TextBox1.Text
            str = str.Trim  ' First remove leading and following spaces from string. 
            Do Until str.IndexOf(" ") = -1 ' If " " isn't in the string then str.IndexOf(" ") will give a value of -1
                IndexOfSpace = str.IndexOf(" ") ' Loop until there are no spaces in string
                str = str.Remove(IndexOfSpace, 1)
            Loop
            NumberOfCharacters = str.Length
            Label1.Text = "There are " & NumberOfCharacters & " characters in " & str & "."
        End Sub
    End Class
    It's a lot easier to just replace the space with a blank string:

    Code:
    TextBox1.Text = TextBox1.Text.Replace(" ","")

  9. #9
    Fanatic Member EntityX's Avatar
    Join Date
    Feb 2007
    Location
    Omnipresence
    Posts
    798

    Re: [RESOLVED] Count string

    You are absolutely right Negaivo. That one line of code does what Trim and the Do Loop does. I wasn't aware of Replace. Is that something new to Visual Basic 2008 or perhpas 2005?
    Make as many mistakes as you can as quickly as you can. We want to make sure that we make a great enough number of mistakes in a given amount of time so that we can be successful.

    "Persistence is the magic of success." Paramahansa Yogananda

Tags for this Thread

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