如何拆分字符串列表以单独显示每个单词?

How can I split a list of strings to display each word individually?

目前我有一个 return 字符串列表的函数。它从一个视图中获取一个单词,然后我想 return 在函数显示不同文本框中的单个单词之后进入视图。但是,我不知道有多少单词会被 returned,因为我不知道用户会输入多少。这只是为了提高我的技能,所以它不会超过五个。我查了一下,并没有真正找到任何有帮助的结果。我考虑过将它拆分到可以工作的视图本身,甚至在我的 ViewModel 中,然后 returning 到视图。但是,就像我说的。我不能简单地放下三个文本框,因为我不确定要输入多少个字。有人可以帮忙吗?我已经在下面发布了我认为可以拆分列表的两个区域。谢谢。

查看:

<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">

<h2>Split Result</h2>
    <!-- Wont work. Fix tomorrow. Split array-->
    <label>Split Words</label>
    <%: Html.TextBox("txtEncodeResult", Model.SplitWords)%>

</asp:Content>

型号:

      Function Split(inSentence) As List(Of String)

            '
            ' Get the posted information from the form
            Dim sSentence As String = inSentence
            '
            ' Create a list of string to put the sentene in and create a new instance of it
            Dim sSplitWords As List(Of String) = New List(Of String)
            '
            ' Find the position of the first space in sSentence
            Dim iPos As Integer
            iPos = InStr(sSentence, " ")

            'Find the length of the sentence
            Dim iLen As Integer
            iLen = Len(sSentence)

            '
            ' Create the remaining length
            Dim iRemainingLength As Integer = 0

            '
            ' Create sProp as string and set sProp to equal sSentence
            Dim sProp As String = ""
            sProp = sSentence

            '
            'Do while the position is not equal to 0
            Do While iPos <> 0

                '
                ' Find the left most characters from the position - 1 in sSentence  and then set this string as sProp
                sProp = Left(sSentence, iPos - 1)

                '
                ' Add the first word to the List
                sSplitWords.Add(sProp)

                '
                ' Find the new remaining length
                iRemainingLength = iLen - iPos

                '
                ' Get the rest of the sentence minus the word which has already been taken away.
                sSentence = sSentence.Substring(iPos, iRemainingLength)

                '
                ' Find the new position of the space in sSentence
                iPos = InStr(sSentence, " ")

                '
                ' Find the length of sSentence
                iLen = Len(sSentence)

                '
                'Loop while the condition is true
            Loop
            If iPos = 0 Then
                sSplitWords.Add(sSentence)
            End If
            '
            ' Return the array
            Return sSplitWords
        End Function

就像我说的,我只是在提高我的编程技能,所以这是非常基础的。谢谢

您可以显着减少Split函数中的代码,实际上减少到一行

Function Split(inSentence) As List(Of String)
    Return (inSentence ?? "").Split(" ").ToList();
End Function

然后在您的视图中(假设您将列表作为模型传递)您可以为每个单词生成一个新的文本框

@For Dim i as Integer = 0 To Model.Count-1
    @: Html.TextBox("tb" & i.ToString(), Model[i])
Next