目前我有一个返回字符串列表的函数。它从视图中获取一个单词然后我想在显示不同文本框中的单个单词的函数之后返回到视图。但是,我不知道将返回多少单词,因为我不知道用户将输入多少单词。它只是为了提高我的技能,所以它将少于五个。我已经看了这个,并且没有真正找到任何有帮助的结果。我想过将它拆分到视图本身上,它可以工作,甚至可以在我的ViewModel中,然后将其返回到视图中。但是,就像我说的那样。我不能简单地放下三个文本框,因为我不确定会输入多少个单词。有人可以帮忙吗?我已经在下面的两个区域发布了我认为我可以拆分列表的地方。谢谢。
查看:
<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
就像我说的,我只是在提高我的编程技巧,所以它非常基础。感谢。
答案 0 :(得分:1)
您可以显着将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