0

Below is the perfect working code, when i googled for splitting the first part of the string with a delimiter.

But i have a problem because, this code works only when the target framework is 4 How can i convert this code, where it will run in target framework 2

    Public Shared Function FirstFromSplit(ByVal source As String, ByVal delimiter As String) As String
        Dim i = source.IndexOf(delimiter)

       Return If(i = -1, source, source.Substring(0, i))

    End Function
5
  • What makes you think this code won't run under .NET 4? Commented Oct 3, 2012 at 16:51
  • why not just use return source.split(delimiter)? Commented Oct 3, 2012 at 16:51
  • @JayRiggs It works in framework 4 but does not work in framework 2. Commented Oct 3, 2012 at 16:52
  • What goes wrong in .NET 2? It should be fine... Commented Oct 3, 2012 at 16:53
  • I don't think If existed until .Net 4.0. Commented Oct 3, 2012 at 16:53

1 Answer 1

1

As far as know both string-methods exists in 2.0, so I guess it's the return-statement that gives troubles. Try re-writing it in the old style.

Public Shared Function FirstFromSplit(ByVal source As String, ByVal delimiter As String) As String 
     Dim i = source.IndexOf(delimiter) 

     If i < 0 Then
       return source
     Else
       return source.Substring(0, i)
     End if
End Function 
Sign up to request clarification or add additional context in comments.

1 Comment

wouldn't return source.split(delimiter)(0) accomplish the same thing? what am I missing? **Edit: I guess the main difference is split doesn't work(very easily at least) with strings as delimiters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.