Results 1 to 6 of 6

Thread: Left/Right string editing quesiton;

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    67

    Exclamation Left/Right string editing quesiton;

    hey all, how do i suing Left/Right commands in vb.net2(2005 express)?
    cuz i c its not the same like in other visual basics..
    in the old versions it was:
    msg$ = left$(msg$,len(msg$)-5)
    but how it works in the vb.net2?

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Left/Right string editing quesiton;

    In .NET 2003 there is no longer "Left" or "right" function
    It is now the SubString Functionm that replace it.
    And I think it is the same in 2005

    VB Code:
    1. Dim MyString As String = "HELLO"
    2.  
    3. Dim MyString2 As String = MyString.Substring(0, 2)    '"HE" equivalent of Left
    4. Dim MyString2 As String = MyString.Substring(4, 2)        '"LO" equivalent of right
    Using VS 2010 on Fw4.0

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Left/Right string editing quesiton;

    You still have the left and right, all of your strings functions are in the strings namespace.. like Strings.Left, Strings.Right, Strings.Mid, as well as others. Substring has better performance, though, in a benchmark I made, it ended up being about 20% more efficient. I will go ahead and repost the benchmark code as well...
    VB Code:
    1. 'For Strings functions
    2.         Dim MyTime As New DateTime
    3.         MyTime = Date.Now
    4.         Dim X As Integer
    5.         For X = 0 To 1000000
    6.             Dim Mystring As String = "LeftMiddleRight"
    7.             Dim MyLeft As String = Strings.Left(Mystring, 4)
    8.             Dim MyRight As String = Strings.Right(Mystring, 5)
    9.             Dim MyMid As String = Strings.Mid(Mystring, 5, 6)
    10.         Next
    11.         Dim EndTime As New DateTime
    12.         EndTime = Date.Now
    13.         Dim TotalTime As New TimeSpan
    14.         TotalTime = EndTime.Subtract(MyTime)
    15.         MsgBox(TotalTime.TotalMilliseconds)
    16.         'averaged about 312 milliseconds
    17.  
    18. 'for substring
    19.         Dim MyTime As New DateTime
    20.         MyTime = Date.Now
    21.         Dim X As Integer
    22.         Dim Y As Integer = 0
    23.         Dim Z As Integer = 10
    24.         Dim A as Integer = 4
    25.         For X = 0 To 1000000
    26.             Dim Mystring As String = "LeftMiddleRight"
    27.             Dim MyLeft As String = Mystring.Substring(0, 4)
    28.             Dim MyRight As String = Mystring.Substring(10, 5)
    29.             Dim MyMid As String = Mystring.Substring(4, 6)
    30.         Next
    31.         Dim EndTime As New DateTime
    32.         EndTime = Date.Now
    33.         Dim TotalTime As New TimeSpan
    34.         TotalTime = EndTime.Subtract(MyTime)
    35.         MsgBox(TotalTime.TotalMilliseconds)
    36.         'averaged about 250 milliseconds

  4. #4
    Addicted Member
    Join Date
    Nov 2005
    Posts
    183

    Re: Left/Right string editing quesiton;

    So...does SubString work in the same way Mid does? As in:

    Mid(String, StartPosition, Length)
    and
    String.Substring(StartPosition, Length)

    ?

  5. #5
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Left/Right string editing quesiton;

    Quote Originally Posted by DragonQ
    So...does SubString work in the same way Mid does? As in:

    Mid(String, StartPosition, Length)
    and
    String.Substring(StartPosition, Length)

    ?
    Yes, but Mid is from the Microsoft.VisualBasic Namespace, witch, a purist like me, do not use .

    But there is nothing bad in using it.
    Using VS 2010 on Fw4.0

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Left/Right string editing quesiton;

    Mid and Substring have one glaring difference. Mid is 1-based while Substring is 0-based, i.e. Mid assumes that first character is at index 1 (VB6 convention I believe) while Substring assumes that the first character is at index 0 (.NET convention).

    My opinion of Runtime functions is swinging again so wossname, if you're reading this, I'm prepared to accept that they are "legacy" functions now, but I'll still use the term "Runtime" functions. There are still some that are useful that have no direct .NET equivalent but I can't see a justifiable reason for using those that do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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