搜索后,我仍然无法使此回文器正常工作 [英] after searching, i still cannot get this palindrome to work

查看:66
本文介绍了搜索后,我仍然无法使此回文器正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它将比较但不会删除标点符号.我将包括所有代码,包括我注释掉的代码,这些代码无法正常工作.任何帮助都是极好的.谢谢!

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


推荐答案

字符串类具有函数string.Replace(oldchar,new char),该函数将所有出现的char替换为新的char,如果那是您的意思然后不需要检查字符的索引.

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.

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



在上面的代码中,您正在用一个空字符串替换一个char.这是不允许的(因为CTS在所有.net语言中都是通用的).您需要做的是将char数组更改为字符串数组,然后在坏char/strings的数组循环中使用Input.Replace(",","");

改进的答案
-----------------
注意:我的vb转换可能不准确,C#可以.

首先让我举一个删除标点符号的例子.

可以将受限数组定义为string[] BadChar=new string[5]{",", ":", ";", ".", "''"};Dim BadChar() As String = New String() {",", ":", ";", ".", "''"}(在vb中).



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).

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();
 }



我在这里使用字符串生成器,因为它包含可变字符串.立即尝试Vb转换.



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

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



现在回答您的评论.

我要求使用字符串数组,因为Replace函数的签名具有Replace(oldchar, newchar)Replace(oldstring,newstring)而不是Replace(oldchar,newstring).在循环中,您得到一个字符,但是尝试用字符串替换,如Input = Input.Replace(c, "")所示.错了.

第二件事,字符必须用单引号引起来.不是双引号.在您的代码中,Dim BadChar() As Char = New Char() {",", ":", ";", ".", "''"}就是这样.我不确定VB允许这样做,但不是可取的方式.尽管您将其更改为单引号,但对于字符''",您需要使用转义字符,例如''\''''.这将再次使您感到困惑.因此,我问了一下,只需使用标点符号的字符串数组即可.

希望现在清楚了.

尝试我的示例.



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.


这篇关于搜索后,我仍然无法使此回文器正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆