1 Attachment(s)
[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.
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)
Re: String.Split with Enters and Tabs
VB.NET 2003 doesn't seem to like StringSplitOptions.None
:(
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)
Re: String.Split with Enters and Tabs
Cool. Thanks! :thumb:
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?
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.