我的For循环跳过我的IF语句

时间:2014-09-25 10:09:30

标签: vb.net if-statement for-loop

我想知道是否有人可以提供帮助。我正在制作一个将Text转换为ASCII的程序。但是,我希望我的程序忽略空格。因此," IT是A"应该是这样的:7384 876583 65

当我使用VB的Step Into Feature时,我可以看到我的For循环正在跳过我的IF语句,它应该给我空间。我不明白为什么。你可能会说,我是初学者,所以任何具体的帮助将不胜感激。我的代码如下所示:

    Dim PlainText, ConvertedLetter As String
    Dim LetterToConvert As Char
    Dim AscNumber, Counter As Integer
    ConvertedLetter = ""
    PlainText = txtPlain.Text
    For Counter = 1 To Len(PlainText)
        LetterToConvert = Mid(PlainText, Counter, 1)
        If PlainText = " " Then
            ConvertedLetter = " "
        Else : AscNumber = Asc(LetterToConvert)
            ConvertedLetter = ConvertedLetter & AscNumber
        End If
    Next
    txtAscii.Text = ConvertedLetter

2 个答案:

答案 0 :(得分:2)

因为你要将整个字符串的PlainText与"进行比较。 &#34 ;.它需要是:

如果LetterToConvert =" "然后....

答案 1 :(得分:1)

试试这个:

Dim PlainText, ConvertedLetter As String
ConvertedLetter = ""
PlainText = "IT WAS A"
For Each c As Char In PlainText 'iterate through each character in the input
    If c <> " " Then ' check whether c is space or not
        ConvertedLetter &= Asc(c).ToString()' ascii value is taken if c<>" "
    Else
        ConvertedLetter &= " " ' c is space means add a space
    End If
Next
MsgBox(ConvertedLetter) ' display the result

您将获得输出

7384 876583 65