|
-
Feb 9th, 2012, 07:38 PM
#1
Thread Starter
New Member
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.
-
Feb 9th, 2012, 07:49 PM
#2
Re: Strings
you could use regex.replace:
vb Code:
Imports System.Text.RegularExpressions
Public Class Form1
Dim number As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
number = numberTextBox.Text.Trim
number = Regex.Replace(number, "-|\s|\(|\)", "")
numberLabel.Text = number
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 9th, 2012, 08:04 PM
#3
Thread Starter
New Member
Re: Strings
 Originally Posted by .paul.
you could use regex.replace:
vb Code:
Imports System.Text.RegularExpressions
Public Class Form1
Dim number As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
number = numberTextBox.Text.Trim
number = Regex.Replace(number, "-|\s|\(|\)", "")
numberLabel.Text = number
End Sub
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.
-
Feb 9th, 2012, 08:26 PM
#4
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 9th, 2012, 08:44 PM
#5
Thread Starter
New Member
Re: Strings
 Originally Posted by .paul.
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.
-
Feb 9th, 2012, 08:48 PM
#6
Re: Strings
 Originally Posted by itJoe
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 9th, 2012, 08:58 PM
#7
Thread Starter
New Member
Re: Strings
 Originally Posted by .paul.
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.
-
Feb 9th, 2012, 09:07 PM
#8
Re: Strings
actually i found it now. sorry about the confusion... not too familiar with the LIKE operator:
vb Code:
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 = numChars To 0 Step -1
' search string to examine if character
' is a hyphen, space or parentheses
If number.Substring(lp, 1) Like "[- ()]" Then number = number.Remove(lp, 1)
Next
numberLabel.Text = number
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 9th, 2012, 09:26 PM
#9
Thread Starter
New Member
Re: Strings
 Originally Posted by .paul.
actually i found it now. sorry about the confusion... not too familiar with the LIKE operator:
vb Code:
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 = numChars To 0 Step -1
' search string to examine if character
' is a hyphen, space or parentheses
If number.Substring(lp, 1) Like "[- ()]" Then number = number.Remove(lp, 1)
Next
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.
-
Feb 9th, 2012, 09:34 PM
#10
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:
dim testString as string = "abc"
testString.substring(1) = "bc"
testString.substring(1, 1) = "b"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 9th, 2012, 09:38 PM
#11
Thread Starter
New Member
Re: Strings
 Originally Posted by .paul.
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:
dim testString as string = "abc"
testString.substring(1) = "bc"
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|