Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
it will compare but not remove the punctuation. I will include all the code including the code that i commented out that did not work correctly. any help would be awesome. thanks!

<pre lang="vb">Public Class frmMain

    Private Sub txtInput_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtInput.TextChanged

    End Sub

    Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
        Dim IsPaly As String
        Dim Input As String = Trim(txtInput.Text.ToUpper)
        Dim BadChar() As Char = New Char() {",", ":", ";", ".", "'"}
        Dim Output As String


        Output = CStr(removePunctuation(Input, BadChar)).ToUpper

        IsPaly = IsPalindrome(Input)

        If IsPaly = True Then
            lblOutput.Text = "This is a Palindrome!"
        Else
            lblOutput.Text = " This is NOT a Palindrome!"
        End If

    End Sub
    Function removePunctuation(ByVal Input As String, ByRef BadChar As Char()) As String

        'For i = 1 To Input.Length
        '    Input = Mid(Input, i, 1)
        '    If InStr(BadChar, Input) Then
        '        Mid(Input, i, 1) = ""
        '    End If
        'Next i

        For Each c As Char In BadChar
            If Input.IndexOf(c) >= 0 Then
                Input = Input.Replace(c, "")
            End If
        Next
        Return Input
    End Function


    '********** this was copiedfrom the internet to test because I ran out of options and found this but STILL did not work!**************

    'Dim tempLetter As String = ""
    'Dim m As Integer, temp As String = ""
    'm = Input.Length
    'For i As Integer = 0 To m - 1 Step 1
    '    temp = Input.Substring(i, 1)

    '    If temp <> "[^A-Za-z]+" Then
    '        '(temp <> ("!")) And (temp <> ("?")) And (temp <> (".")) And (temp <> (",")) And (temp <> ("'")) _
    '        'And (temp <> ("`")) And (temp <> (":")) And (temp <> (";")) And (temp <> (" ")) Then
    '        tempLetter &= temp

    '    End If
    'Next i

    '    Input = tempLetter

    '    Return Input
    'End Function


    Function IsPalindrome(ByRef Output As String) As String

        If Output = StrReverse(Output) Then
            IsPalindrome = True
        Else
            IsPalindrome = False
        End If

        Return IsPalindrome
    End Function



    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        lblOutput.Text = "Enter another word to test."
        txtInput.Clear()

    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

End Class

Posted
Comments
Albin Abel 15-Mar-11 1:31am    
is Vb.Net allows chars in double quotes? Dim BadChar() As Char = New Char() {",", ":", ";", ".", "'"}
Albin Abel 15-Mar-11 14:35pm    
I posted an improved answer as per your comment

1 solution

the string class has a function string.Replace(oldchar,new char) which replaces all the occurrences of that char with new char, if that is what you want then no need to check the index of the chars.

VB
For Each c As Char In BadChar
     If Input.IndexOf(c) >= 0 Then
         Input = Input.Replace(c, "")
     End If
 Next


In the above code you are replacing a char with a empty string. This is not allowed (as CTS is common for all .net languages). What you need to do is change your char array to string array then use the Input.Replace(",",""); in the array loop of the bad chars/strings

Improved answer
-----------------
Note: My vb conversion may not be accurate, the C# would be fine.

First let me give you an example for remove punctuation.

can define limited array as string[] BadChar=new string[5]{",", ":", ";", ".", "'"}; , Dim BadChar() As String = New String() {",", ":", ";", ".", "'"} (in vb).

C#
public string removePunctuation(string input, string[] badChars)
 {
     StringBuilder sb = new StringBuilder();
     sb.Append(input);

     foreach (string badChar in badChars)
     {
         sb.Replace(badChar, "");
     }
     return sb.ToString();
 }


I am using string builder here as it is holds a mutable string. Trying Vb conversion now.

VB
Function removePunctuation(ByVal Input As String, ByVal BadChar As String()) As String
     Dim sb as new StringBuilder()
     sb.Append(Input);
     ForEach badChar as string In badChars
         sb.Replace(badChar,"")
     End
     Return sb.Tostring()

End Function


Now answers for your comment.

I asked to use a string array, because the signature of the Replace function has either Replace(oldchar, newchar) or Replace(oldstring,newstring) and not Replace(oldchar,newstring). In you loop you are getting a char, but trying to replace with a string as here Input = Input.Replace(c, ""). That was wrong.

Second thing the chars has to be enclosed with a single quote. Not double quote. In your code Dim BadChar() As Char = New Char() {",", ":", ";", ".", "'"} was like this. I am not sure VB allow this, but yet not preferable way. Though you change it to single quotes then for the char "'" you need to use an escape character like '\''. That will again confuse you. So that I asked simply use a string array of punctuation marks.

Hope it is clear now.

try my example.
 
Share this answer
 
v2
Comments
fjdiewornncalwe 15-Mar-11 13:53pm    
OP's comment moved from answer:
"In the above code you are replacing a char with a empty string. This is not allowed (as CTS is common for all .net languages). What you need to do is change your char array to string array then use the Input.Replace(",",""); in the array loop of the bad chars/strings "

anyway you can be a bit more detailed in this? i am still learning vb.
Espen Harlinn 15-Mar-11 15:10pm    
Nice effort, my 5
Albin Abel 15-Mar-11 15:29pm    
Thanks Espen Harlinn!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900