|
-
Dec 5th, 2005, 09:12 AM
#1
Thread Starter
Lively Member
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?
-
Dec 5th, 2005, 09:30 AM
#2
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:
Dim MyString As String = "HELLO"
Dim MyString2 As String = MyString.Substring(0, 2) '"HE" equivalent of Left
Dim MyString2 As String = MyString.Substring(4, 2) '"LO" equivalent of right
-
Dec 5th, 2005, 11:57 AM
#3
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:
'For Strings functions
Dim MyTime As New DateTime
MyTime = Date.Now
Dim X As Integer
For X = 0 To 1000000
Dim Mystring As String = "LeftMiddleRight"
Dim MyLeft As String = Strings.Left(Mystring, 4)
Dim MyRight As String = Strings.Right(Mystring, 5)
Dim MyMid As String = Strings.Mid(Mystring, 5, 6)
Next
Dim EndTime As New DateTime
EndTime = Date.Now
Dim TotalTime As New TimeSpan
TotalTime = EndTime.Subtract(MyTime)
MsgBox(TotalTime.TotalMilliseconds)
'averaged about 312 milliseconds
'for substring
Dim MyTime As New DateTime
MyTime = Date.Now
Dim X As Integer
Dim Y As Integer = 0
Dim Z As Integer = 10
Dim A as Integer = 4
For X = 0 To 1000000
Dim Mystring As String = "LeftMiddleRight"
Dim MyLeft As String = Mystring.Substring(0, 4)
Dim MyRight As String = Mystring.Substring(10, 5)
Dim MyMid As String = Mystring.Substring(4, 6)
Next
Dim EndTime As New DateTime
EndTime = Date.Now
Dim TotalTime As New TimeSpan
TotalTime = EndTime.Subtract(MyTime)
MsgBox(TotalTime.TotalMilliseconds)
'averaged about 250 milliseconds
-
Dec 5th, 2005, 01:07 PM
#4
Addicted Member
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)
?
-
Dec 5th, 2005, 01:13 PM
#5
Re: Left/Right string editing quesiton;
 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.
-
Dec 5th, 2005, 06:26 PM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|