Reputation: 72
At the moment I have a function which returns a list of strings. It takes a word from a view and then I would like to return to the view after the function displaying the individual words in different text boxes. However, I will not know how many words will be returned as I don't know how many the user will enter. It's only for improving my skills so it will be less than five. I have looked this up and haven't really found any results which help. I thought about splitting it up on the view itself which could work or even in my ViewModel and then returning that to the view. However, like I said. I can't simply put down three text boxes as I don't know for sure how many words will be entered. Can anybody help? I have posted the two areas below where I think I can split the list. Thank you.
View:
<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>
Model:
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
Like I said, I am simply improving my programming skills so it is very basic. Thanks.
Upvotes: 0
Views: 138
Reputation: 82096
You can dramatically reduce the code in your Split
function, to one line in fact
Function Split(inSentence) As List(Of String)
Return (inSentence ?? "").Split(" ").ToList();
End Function
Then in your view (assuming you are passing the list as the model) you can generate a new text box per word
@For Dim i as Integer = 0 To Model.Count-1
@: Html.TextBox("tb" & i.ToString(), Model[i])
Next
Upvotes: 1