CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: HELP on Strings

  1. #1
    Join Date
    Jun 2012
    Posts
    6

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2012
    Posts
    6

    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

  4. #4
    Join Date
    Jun 2012
    Posts
    6

    Re: HELP on Strings

    THis doesn't work, exactly how i want it. It doesn't Split the words it splits the chars

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  6. #6
    Join Date
    Jun 2012
    Posts
    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?

  7. #7
    Join Date
    Jun 2012
    Posts
    6

    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

  8. #8
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    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.

  9. #9
    Join Date
    Jun 2012
    Posts
    6

    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
  •  





Click Here to Expand Forum to Full Width

Featured