Results 1 to 6 of 6

Thread: [RESOLVED] String.Split with Enters and Tabs

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved [RESOLVED] String.Split with Enters and Tabs

    Hello.

    I'm getting information from the CLipboard that looks like the attached picture.

    As you can see, there are 2 columns.

    I'd like to be able to split each column into a separate array. This is what I have tried :

    Code:
            Dim iData As IDataObject = New DataObject
            iData = Clipboard.GetDataObject()
    
            If iData.GetDataPresent(DataFormats.Text) Then
    
                strClipText = iData.GetData(DataFormats.Text, True).ToString().Split(Environment.NewLine.ToCharArray())
    
    
                For iCounter = 0 To strClipText.Length - 1
                    strClipText(iCounter) = strLeft(strClipText(iCounter), 5)
                    strClipText(iCounter) = Trim(strClipText(iCounter))
    
                Next
            End If
    
        Public Function strLeft(ByVal param As String, ByVal length As Integer) As String
    
            'we start at 0 since we want to get the characters starting from the
            'left and with the specified lenght and assign it to a variable
            Dim result As String
            If IsNumeric(param) Then result = Trim(param.Substring(0, length))
    
            'return the result of the operation
            Return result
    
        End Function
    The problem with this is that I still get the entire row, instead of the first 5 characters of the row. I'd also like to know if it is possible to not include empty spaces - such as the first row??

    Any help would be appreciated.
    Attached Images Attached Images  

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: String.Split with Enters and Tabs

    Try this:

    Code:
    strClipText = _
      iData.GetData(DataFormats.Text, True).ToString().Split(New String() _ 
      {Environment.NewLine, ControlChars.Tab}, _ 
      StringSplitOptions.None)

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: String.Split with Enters and Tabs

    VB.NET 2003 doesn't seem to like StringSplitOptions.None


  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: String.Split with Enters and Tabs

    My fault, didn't look at the version.

    The problem is that Environment.NewLine is actually a String having CR (0xD) and LF (0xA) characters.
    You need a string as a separator, otherwise Split function will try to separate CRs and LFs.
    Create a string array, not char.
    Code:
    Dim Separators(1) As String
    
    Separators(0) = Environment.NewLine
    Separators(1) = ControlChars.Tab
    
    iData.GetData(DataFormats.Text, True).ToString().Split(Separators)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: String.Split with Enters and Tabs

    Cool. Thanks!

    I've implemented it like this :

    Code:
            If iData.GetDataPresent(DataFormats.Text) Then
    
                strClipText = iData.GetData(DataFormats.Text, True).ToString().Split(Separators(0))
                strClipText2 = iData.GetData(DataFormats.Text, True).ToString().Split(Separators(1))
    
            End If
    Last question, hopefully.

    Seeing the fact that I have only one array here containing everything. Is there a way to disregard the empty elements? Meaning that every second item now is empty, how can I determine that?

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: String.Split with Enters and Tabs

    Well, in some later versions of the Framework there is StringSplitOptions.RemoveEmptyEntries option, but here you should do it manually by checking each element of the resulting array and removing it.

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