-
June 18th, 2012, 12:27 PM
#1
HELP on Strings
I need some help.
I've tried a lot of stuff but just don't seem to be able to solve my problem
I have a rather long string, i want it to be divided every 8 words into a new line.
I've tried substringing, tried cutting the string into 8 word array strings. but that didn't work to good. so PLEASE if someone can help me
-
June 18th, 2012, 12:51 PM
#2
Re: HELP on Strings
Find a common delimiter, and if one doesn't exist, CREATE ONE in the first step.
If it does, skip first step, and SPLIT on that character into ARRAY().
Then, choose 8 and combine into one.
-
June 18th, 2012, 01:42 PM
#3
Re: HELP on Strings
Thank you very much it works now this is what i did.
Code:
'Adding the ParamArray Seperator
Const charNumber = 8
Dim oldString = userinput.text
Dim sb As New System.Text.StringBuilder()
For i As Integer = 0 To oldString.Length - 1
If i Mod charNumber = 0 Then
sb.Append("/"c)
End If
sb.Append(oldString(i))
Next
Dim newString = sb.ToString()
'The Split
Dim s As String = newString
Dim words As String() = s.Split(New Char() {"/"c})
Dim part As String
For Each part In words
Output. text = Output.text & vbNewLine & part
Next
-
June 18th, 2012, 01:45 PM
#4
Re: HELP on Strings
THis doesn't work, exactly how i want it. It doesn't Split the words it splits the chars
-
June 18th, 2012, 10:43 PM
#5
Re: HELP on Strings
Try searching. First hit:
Code:
Module Module1
Sub Main()
' We want to split this input string
Dim s As String = "there is a cat"
' Split string based on spaces
Dim words As String() = s.Split(New Char() {" "c})
' Use For Each loop over words and display them
Dim word As String
For Each word In words
Console.WriteLine(word)
Next
End Sub
End Module
Notice words is the array. Word is the split output
Last edited by dglienna; June 18th, 2012 at 10:45 PM.
-
June 19th, 2012, 06:29 AM
#6
Re: HELP on Strings
Thanks, still it doesn't solve how i'm gunna let the ouput show 8 words on a line then a newline and then 8 words again, ect. ???
Must i take the array made at the split and make it add only 8 words to the string and then other 8 again?
And how am i gunna do it?
-
June 19th, 2012, 08:49 AM
#7
Re: HELP on Strings
Thanks for the help. I got everything fixed.
This is my final code thats working
Code:
Dim i As Integer
' Split The string
Dim s As String = userinput.text
' Split string based on spaces
Dim words As String() = s.Split(New Char() {" "c})
' Use For Each loop over words and display them
Dim word As String
For Each word In words
If i = 8 Or i = 16 Or i = 32 Or i = 40 Then
output.text = output.text& vbNewLine
Else
End If
i = i + 1
output.text = output.text & " " & word
Next
-
June 19th, 2012, 01:48 PM
#8
Re: HELP on Strings
Very good. But what if you have over 40 words???
Code:
Dim i As Integer
' Split The string
Dim s As String = userinput.text
' Split string based on spaces
Dim words As String() = s.Split(New Char() {" "c})
' Use For Each loop over words and display them
Dim word As String
For Each word In words
If i = 8 Then
output.text = output.text& vbNewLine
i = 0
Else
End If
i = i + 1
output.text = output.text & " " & word
Next
Also, since you are building a new string, you might want to look into the "StringBuilder" object.
-
June 20th, 2012, 03:17 AM
#9
Re: HELP on Strings
Yeah, you got a point there. thanks i'm changing the integer part.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|