Separate a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ze Coder

    #1

    Separate a string

    Hello, I would like to know how to split a string variable in x
    lines(possibly an array) of a maximum length of 30 chrs, but without
    truncating words.

    I Think there's a trick to do with substring, but don't know much.

    Thanks to help me on this issue,

    Sebastien


  • Ronchese

    #2
    Re: Separate a string

    dim arrString() as string

    'this code splits your string in a array, word by word, separated by space:
    arrString = "your string".split(" ")


    If you need your string be approximated to 30 chars, you can do some loops
    and concatenate the array elements on a second array. For sample:

    Dim i As Integer
    Dim strLine As String
    Dim arrString2() As String
    Dim intUboundStr2 As Integer = -1

    'transfer all strings from first array, to second array,
    'trying to limite each line till 30 characters
    For i = 0 To arrString.Lengt h - 1
    If Len(strLine & arrString(i)) <= 30 Then
    strLine = strLine & " " & arrString(i)
    Else
    intUboundStr2 = intUboundStr2 + 1
    ReDim Preserve arrString2(intU boundStr2)
    arrString2(intU boundStr2) = strLine
    strLine = ""
    End If
    Next
    'transfer the eventual remanescent characters
    If strLine <> "" Then
    intUboundStr2 = intUboundStr2 + 1
    ReDim Preserve arrString2(intU boundStr2)
    arrString2(intU boundStr2) = strLine
    End If
    End Sub


    Note: you can use StringBuilder class to eventually help your code speed up,
    incase you are working with large strings.


    []s
    Cesar




    "Ze Coder" <zecoder@nospam .com> escreveu na mensagem
    news:evVgFH3nFH A.3608@TK2MSFTN GP15.phx.gbl...
    Hello, I would like to know how to split a string variable in x
    lines(possibly an array) of a maximum length of 30 chrs, but without
    truncating words.

    I Think there's a trick to do with substring, but don't know much.

    Thanks to help me on this issue,

    Sebastien




    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Separate a string

      Ze,

      Another method than Ronchese avoiding the redim which is eating resources in
      his sample.
      (Ronchese uses a classic VB approach, with what is nothing wrong except than
      that Redim is not so lucky).

      It is typed in this message and not tested so watch typos or whatever

      \\\
      Dim startpoint as integer
      dim stringlength as integer = mystring.length
      dim count as integer = stringlength \ 30
      dim ar as new arraylist
      For i as integer = 0 to count
      ar.add(mystring .substring(star tpoint,30))
      startpoint =+ 30
      next
      ar.add(mystring .substring(star tpoint)
      ///

      I hope this helps,

      Cor


      Comment

      • Ze Coder

        #4
        Re: Separate a string

        Thanks to both of you!


        "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> a écrit dans le message de
        news: #46deo8nFHA.344 8@TK2MSFTNGP12. phx.gbl...[color=blue]
        > Ze,
        >
        > Another method than Ronchese avoiding the redim which is eating resources[/color]
        in[color=blue]
        > his sample.
        > (Ronchese uses a classic VB approach, with what is nothing wrong except[/color]
        than[color=blue]
        > that Redim is not so lucky).
        >
        > It is typed in this message and not tested so watch typos or whatever
        >
        > \\\
        > Dim startpoint as integer
        > dim stringlength as integer = mystring.length
        > dim count as integer = stringlength \ 30
        > dim ar as new arraylist
        > For i as integer = 0 to count
        > ar.add(mystring .substring(star tpoint,30))
        > startpoint =+ 30
        > next
        > ar.add(mystring .substring(star tpoint)
        > ///
        >
        > I hope this helps,
        >
        > Cor
        >
        >[/color]


        Comment

        Working...