Results 1 to 5 of 5

Thread: String problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    Brossard, Québec, Canada
    Posts
    241
    I have this address that comes from a user input in a TEXTAREA (memo field) on a web page... I put this string which contains an unknow number of linefeeds (could be 0 to infinite)... I need to write this address to a file but makin it fit in 4 lines each time.

    Even if my variable (the address) contains 0 line feeds, I need the other 3 lines to be blank.

    To the other extreme, if my address was entered on 10 lines (just for exemple) I need to have only 4 line. I would like to have the fisrt 3 line as they are and the rest of the lines (4 thru 10) on only one line.

    Hope I was clear. Can this be done?

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Of course it can be done

    The issue is how you break it down in such a way that you can actually do it.

    The linefeeds are usually Chr$(13) or the VB constant vbCR. So all you need to do is determine when the next one occurs and then do what is necessary when you find it.

    So write what they call "pseudo-code" that explains the process each step of the way and what needs to happen if no more line feeds are found and then turn it into code.

    The main idea is to break the problem down into smaller problems :

    1. How do I find linefeeds?
    2. How do I get rid of them if there are more?
    3. What kind of loop do I go through?

  3. #3
    Addicted Member
    Join Date
    Jan 2000
    Location
    Sydney, Australia
    Posts
    196
    Heres some quick and nast code i've put together - if it doesn't exactly do what you meant then it might at least go close...



    Code:
    Private Sub Command1_Click()
        Dim TempStr As String
        Dim i As Integer
        Dim myArray() As String
    
        'Get the string from the textarea (textbox)
        TempStr = Text1.Text
    
        'Create MyArray by delimiter cariiage return line feed
        myArray = Split(TempStr, vbCrLf)
    
        'Num elements in the new array
        Dim NumberOfElements As Integer
        NumberOfElements = UBound(myArray) + 1
    
        If NumberOfElements < 4 Then
            
            'Not enough lines entered in textbox
            ReDim Preserve myArray(3) 'extend array to 4 elements (0 to 3)
            For i = NumberOfElements To 3
                myArray(i) = "" 'initialise to 0-length string the new elements
            Next i
        
        ElseIf NumberOfElements > 4 Then
            
            'too many lines entered in textbox
            For i = 4 To UBound(myArray)
                'Add all the extral lines to the 4th line
                myArray(3) = myArray(3) & " " & myArray(i)
            Next i
            ReDim Preserve myArray(3) 'Reduce the array back to 4 elements
        
        End If
    
        'Put the string back together
        TempStr = myArray(0) & vbCrLf & _
        myArray(1) & vbCrLf & _
        myArray(2) & vbCrLf & _
        myArray(3)
    
        'Reassign it to the textbox
        Text1.Text = TempStr
    
    End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 1999
    Location
    Brossard, Québec, Canada
    Posts
    241

    Thumbs up Thanks

    Thanks a lot guys!

    I did not know of the split function

    Very helpfull replies

  5. #5
    Addicted Member
    Join Date
    Jan 2000
    Location
    Sydney, Australia
    Posts
    196
    i think Split() is only for vb6 - but yeah it works great for string manipulations...

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