Results 1 to 11 of 11

Thread: Strings

  1. #1

    Thread Starter
    New Member itJoe's Avatar
    Join Date
    Feb 2012
    Location
    Tri-Cities, WA
    Posts
    8

    Strings

    Hi everyone,

    this is my first post to this forum.

    Currently, I am a full time student hoping to graduate in June with a two year degree.

    One of the classes I am taking is Visual Basic 2 and am having a bit of trouble with strings. Here is the click event I have so far. The red text is the problem spot. My instructor thought it should work.

    Private Sub displayButton_Click(sender As System.Object, e As System.EventArgs) Handles displayButton.Click

    Dim number As String
    number = numberTextBox.Text.Trim

    ' count how many characters in the string
    Dim numChars As Integer = number.Length

    numChars = numChars - 1

    ' start loop using the total number of characters
    For lp As Integer = 0 To numChars

    ' search string to examine if character
    ' is a hyphen, space or parentheses
    If number.Substring(lp) Like "[- ()]" Then number = number.Remove(lp, 1)
    Next

    numberLabel.Text = number

    End Sub


    Can anyone tell me why this isn't working? I am stumped.

    Thanks!

    Joe.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Strings

    you could use regex.replace:

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.     Dim number As String
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         number = numberTextBox.Text.Trim
    8.         number = Regex.Replace(number, "-|\s|\(|\)", "")
    9.         numberLabel.Text = number
    10.     End Sub
    11.  
    12. End Class

  3. #3

    Thread Starter
    New Member itJoe's Avatar
    Join Date
    Feb 2012
    Location
    Tri-Cities, WA
    Posts
    8

    Re: Strings

    Quote Originally Posted by .paul. View Post
    you could use regex.replace:

    vb Code:
    1. Imports System.Text.RegularExpressions
    2.  
    3. Public Class Form1
    4.     Dim number As String
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         number = numberTextBox.Text.Trim
    8.         number = Regex.Replace(number, "-|\s|\(|\)", "")
    9.         numberLabel.Text = number
    10.     End Sub
    11.  
    12. End Class
    Wow! Very cool! With a little tweaking I got this to work!

    But...now here's the thing...why wouldn't my code work? What am I doing wrong? The lesson is about using 'like' so...any ideas why?



    Joe.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Strings

    here's some information from msdn:

    http://msdn.microsoft.com/en-us/library/swf8kaxw.aspx

  5. #5

    Thread Starter
    New Member itJoe's Avatar
    Join Date
    Feb 2012
    Location
    Tri-Cities, WA
    Posts
    8

    Re: Strings

    Quote Originally Posted by .paul. View Post
    Yeah, in my Googling I already found that page and read it. If it contains the answer I'm not seeing it. I am still very new at this.

    Thanks anyways.

    Joe.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Strings

    Quote Originally Posted by itJoe View Post
    why wouldn't my code work? What am I doing wrong? The lesson is about using 'like' so...any ideas why?



    Joe.
    i'm not sure, but i think the problem is the hyphen, which is a special character in a like statement, + you're using it as a literal + probably need to escape the hyphen somehow for it to be treated as a literal...

  7. #7

    Thread Starter
    New Member itJoe's Avatar
    Join Date
    Feb 2012
    Location
    Tri-Cities, WA
    Posts
    8

    Re: Strings

    Quote Originally Posted by .paul. View Post
    i'm not sure, but i think the problem is the hyphen, which is a special character in a like statement, + you're using it as a literal + probably need to escape the hyphen somehow for it to be treated as a literal...
    I wondered about that so I even took it out of the click event but still no joy.

    If send you the project files could you run it and have a look?

    Joe.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Strings

    actually i found it now. sorry about the confusion... not too familiar with the LIKE operator:

    vb Code:
    1. Dim number As String
    2. number = numberTextBox.Text.Trim
    3.  
    4. ' count how many characters in the string
    5. Dim numChars As Integer = number.Length
    6.  
    7. numChars = numChars - 1
    8.  
    9. ' start loop using the total number of characters
    10. For lp As Integer = numChars To 0 Step -1
    11.     ' search string to examine if character
    12.     ' is a hyphen, space or parentheses
    13.     If number.Substring(lp, 1) Like "[- ()]" Then number = number.Remove(lp, 1)
    14. Next
    15.  
    16. numberLabel.Text = number

  9. #9

    Thread Starter
    New Member itJoe's Avatar
    Join Date
    Feb 2012
    Location
    Tri-Cities, WA
    Posts
    8

    Re: Strings

    Quote Originally Posted by .paul. View Post
    actually i found it now. sorry about the confusion... not too familiar with the LIKE operator:

    vb Code:
    1. Dim number As String
    2. number = numberTextBox.Text.Trim
    3.  
    4. ' count how many characters in the string
    5. Dim numChars As Integer = number.Length
    6.  
    7. numChars = numChars - 1
    8.  
    9. ' start loop using the total number of characters
    10. For lp As Integer = numChars To 0 Step -1
    11.     ' search string to examine if character
    12.     ' is a hyphen, space or parentheses
    13.     If number.Substring(lp, 1) Like "[- ()]" Then number = number.Remove(lp, 1)
    14. Next
    15.  
    16. numberLabel.Text = number
    WOW! FREAKIN AWESOME!

    So if I'm reading this right, I was starting at the wrong end of the string? That explains a couple of things!

    Thanks!

    Joe.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Strings

    the problem looping from 0 to numChars is that the length of the string changes during the loop so by the time you get to numChars (or probably before) there's no string at that index.

    the reverse loop solves that problem.

    the other problem was the substring code. you weren't specifying to take 1 character. this is how it works:

    vb Code:
    1. dim testString as string = "abc"
    2.  
    3. testString.substring(1) = "bc"
    4. testString.substring(1, 1) = "b"

  11. #11

    Thread Starter
    New Member itJoe's Avatar
    Join Date
    Feb 2012
    Location
    Tri-Cities, WA
    Posts
    8

    Re: Strings

    Quote Originally Posted by .paul. View Post
    the problem looping from 0 to numChars is that the length of the string changes during the loop so by the time you get to numChars (or probably before) there's no string at that index.

    the reverse loop solves that problem.

    the other problem was the substring code. you weren't specifying to take 1 character. this is how it works:

    vb Code:
    1. dim testString as string = "abc"
    2.  
    3. testString.substring(1) = "bc"
    4. testString.substring(1, 1) = "b"
    Ah, I see.

    I actually considered that at one point but I see the way I tried to compensate for it was dead wrong.

    Thank you very much Paul!

    Joe.

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