子字符串未检索到正确的字母

时间:2017-01-29 17:55:21

标签: vb.net

Public Class Form1
    Private Sub btnMono_Click(sender As Object, e As EventArgs) Handles btnMono.Click
        Dim full As String = Me.txtNamw.Text
        Dim Enter As Integer
        Dim LastEnter As Integer

        Dim firstI As String
        Dim secondI As String
        Dim thirdI As String

        Enter = full.IndexOf("") 'Finds the first space
        LastEnter = full.LastIndexOf("") 'Finds the last space

        Dim secondhalf As String = full.Substring(Enter, LastEnter)
        Dim thirdhalf As String = full.Substring(LastEnter, full.Length)

        firstI = full.Chars(0)
        secondI = secondhalf.Chars(0)
        thirdI = thirdhalf.Chars(0)

        Me.lblAnswer.Text = String.Concat(firstI, secondI, thirdI)
    End Sub
End Class

我必须为学校创建一个程序,所以当输入名字,第二个名字和中间名时,所有3个名字的首字母都会出现。我不确定为什么这不起作用。我试图将每个名字分开然后获得他们的第一个名字,但它不起作用。我是VB.NET的新手。

谢谢

1 个答案:

答案 0 :(得分:0)

正如@Plutonix所说,用.Split()方法很容易。

    Dim fName, mName, lName As Char
    Dim names() As String = s.Split()
    fName = names(0).Chars(0)

    mName = names(1).Chars(0)

    lName = names(2).Chars(0)
    Console.WriteLine(fName & " " & mName & " " & lName)