在不使用split函数的情况下,将全名字符串拆分为单独的变量(first,middle和last)

时间:2016-04-03 18:24:10

标签: vb.net string split

所以我被赋予了一个用全名分叉字符串的任务,然后分别打印出名字和姓氏。例如,输入:Steve Robertson,输出:First name: Steve Last name: Robertson

我成功了,这很容易。但是我在将全名字符串分成第一个,最后一个中间时遇到了麻烦。这是我到目前为止所做的。

Sub Main()
        Dim string1 As String = ""
        Dim string2 As String = ""
        Dim string3 As String = ""
        Dim string4 As String = ""
        Dim temp1 As String = ""
        Dim integer1 As Integer = 1
        Console.WriteLine("Enter the string you want to bifurcate: ")
        string1 = Console.ReadLine()
        While integer1 <> 0
            integer1 = InStr(string1, " ")
            Console.WriteLine(string1)
            string2 = Left(string1, integer1)
            Console.WriteLine(string2)
            string3 = Mid(string1, integer1 + 1)
            Console.WriteLine(string3)
            string4 = Mid(string1, integer1 + 1)
            Console.WriteLine(string4)
            string1 = string4
        End While
        Console.WriteLine("First name is: " & string2)
        Console.WriteLine("Second name is: " & string3)
        Console.WriteLine("Third name is: " & string4)
        Console.ReadKey()
    End Sub

请记住,我只打印几乎每个变量,以查看它们在迭代过程中的值。我只能使用len()函数和代码中已有的函数。

编辑:

所以我摆弄并最终得到了这个东西,没有循环,但我想知道是否有一个更清洁/正确的方法来做这个而不重复变量,也不需要创建任何新的。

Sub Main()
    Dim string1 As String = ""
    Dim string2 As String = ""
    Dim string3 As String = ""
    Dim string4 As String = ""
    Dim integer1 As Integer
    Console.WriteLine("Enter the string you want to split: ")
    string1 = Console.ReadLine()
    integer1 = InStr(string1, " ")
    string2 = Left(string1, integer1)
    string3 = Mid(string1, integer1 + 1)
    integer1 = InStr(string3, " ")
    string4 = Left(string3, integer1)
    string3 = Mid(string3, integer1 + 1)
    Console.WriteLine("The first name is: " & string2)
    Console.WriteLine("The middle name is: " & string4)
    Console.WriteLine("The last name is: " & string3)
    Console.ReadKey()
End Sub

2 个答案:

答案 0 :(得分:2)

这是一种方法。循环访问用户输入的字符。继续将它们连接在一起并将它们放入List(Of String),这样就可以很容易地在最后写出...这个帐号适用于多个空格,如果还有多个空格的话名字之间的一个。我还在代码中添加了一些注释,以便更容易理解。

注意:这只是一种方法...... (还有其他方式)

经过审查和测试的代码

    Dim nList As New List(Of String)
    Dim uStr As String = String.Empty

    Console.WriteLine("Enter the string you want to bifurcate: ")
    uStr = Console.ReadLine()
    If Not String.IsNullOrEmpty(uStr) Then 'Make sure something was entered...
        Dim tStr As String = String.Empty

        'Loop through user's input...
        Do Until uStr = String.Empty
            For Each c As Char In uStr
                If Not Char.IsWhiteSpace(c) Then 'If it's a space we can add to the current string...
                    tStr &= c.ToString
                Else
                    'We can assume its another section of the name...
                    Exit For
                End If
            Next
            'If its a space, remove it from current string...
            If String.IsNullOrEmpty(tStr) Then uStr = uStr.Remove(0, tStr.Length + 1) : Continue Do
            'Add the string to the list, could be first name, middle or lastname?
            If nList.Count = 0 Then
                nList.Add("First Name: " & tStr)
            ElseIf nList.Count = 1 Then
                nList.Add("Middle Name: " & tStr)
            ElseIf nList.Count = 2 Then
                nList.Add("Last Name: " & tStr)
            End If

            'Now we can remove what we got from the users input...
            uStr = uStr.Remove(0, tStr.Length)
            tStr = String.Empty
        Loop

    End If

    'Finally write out the values...
    Console.WriteLine(String.Join(Environment.NewLine, nList.ToArray))
    Console.ReadLine()

计划的屏幕截图

enter image description here

答案 1 :(得分:0)

也许是这样的

 Dim fullName = "Juan Perez"
 Dim name = fullName.Substring(0, fullName.IndexOf(" "))
 Dim lastName = fullName.Substring(fullName.IndexOf(" ") + 1)

How to separate full name string

我建议您设计将返回名字和姓氏的扩展程序

  Module Module1

 <Extension()>
 Public Function returnFirst(ByVal fullName As String) As String

    Return fullName.Substring(0, fullName.IndexOf(" "))
End Function

 <Extension()>

 Public Function returnLast(ByVal fullName As String) As String

    Return fullName.Substring(fullName.IndexOf(" ") + 1)

End Function

 End Module

&#39;称之为

'import module1

Dim Name as string = 'FirstName LastName'

MessageBox.Show(NAME.returnFirst)
MessageBox.Show(NAME.returnLast)