locked
Left Function for Strings in VB.Net RRS feed

  • Question

  • User2023521679 posted

    Since the string class does not have a Left method, how would you do this in VB.Net. I could use String.SubString(0, x),  but the problem with this method is that it throws an exception if the String is not at least x characters long.

    Should I use the Microsoft.VisualBasic.Left function instead? 

    Monday, March 15, 2010 2:48 PM

Answers

  • User-1360095595 posted

    I would validate the length of the string, and then use Substring(). 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 15, 2010 3:47 PM

All replies

  • User560403387 posted

    Just use what works. If the Microsoft.VisualBasic.Strings.Left does what you need, use it. If you want a prettier solution, add a 'Left' extension method to the string class.

    Menno

    Monday, March 15, 2010 3:43 PM
  • User-1360095595 posted

    I would validate the length of the string, and then use Substring(). 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 15, 2010 3:47 PM
  • User-1199946673 posted

    Should I use the Microsoft.VisualBasic.Left function instead? 
     

    You could also create your own Left (extension) method:

        <System.Runtime.CompilerServices.Extension()> _
        Public Function Left(ByVal [string] As String, ByVal length As Integer) As String
            If length < [string].Length Then length = [string].Length
            Return [string].Substring(0, length)
        End Function
    


     

    Monday, March 15, 2010 4:04 PM
  • User-952121411 posted

    Should I use the Microsoft.VisualBasic.Left function instead? 
     

    There is nothing wrong or incorrect about using the Microsoft.VisualBasic namespace methods in VB.NET.  In fact even some C# developers, import that namespace and use those functions in their programming too!  I typically try to use the purely .NET namespaces instead of the VB ones, but sometimes if I need to write code quickly, I go back to using the old trusty methods like Right(), Left(), Mid(), etc.

    Friday, March 19, 2010 9:54 AM
  • User2023521679 posted

     Thanks. I new I could use them, I just wasn't sure if the use of these functions were deprecated under .Net.

    I'm surprised that String.Left(...) and String.Right(...) weren't included in the string classes methods.

    Friday, March 19, 2010 10:00 AM
  • User-1996290357 posted

    Hi hans_v

    Just a quick correction to your comparation operator, and the opposite RIGHT function:

    <System.Runtime.CompilerServices.Extension()> _
    Public Function Left(ByVal [string] As String, ByVal length As Integer) As String
        If length > [string].Length Then length = [string].Length
        Return [string].Substring(0, length)
    End Function
    
    <System.Runtime.CompilerServices.Extension()> _
    Public Function Right(ByVal [string] As String, ByVal length As Integer) As String
        If length > [string].Length Then length = [string].Length
        Return [string].Substring([string].Length - length, length)
    End Function
    Saturday, August 27, 2011 12:34 PM