Left, Right and Mid

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

Using C#, what is the equivalent of functions Left, Right and Mid that we
had in vb6?

Thanks,
Alan
 
left is MyString.SubString(0,length)
mid is MyString.SubString(start, length)
right is MyString.SubString(MyString.Length - length)
 
as well as the answers the other guys have provided, you can also import the
visualbasic lib and actually use left, right and mid in c'

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 
string myString = "My String";
Left: myString.Substring(0, 4); // Left 4 characters
Right: myString.Substring(myString.Length - 4); // Right 4 characters
Mid: myString.Substring(startIndex); // Mid overload 1
Mid: myString.Substring(startIndex, count); // Mid overload 2

Mid has optional parameters, so these act as overloads in C#. The Substring
method can easily handle all formats of the VB methods. You will have to
convert
from using 1 based indexing to 0 based indexing, since the VB functions use 1 to
denote the first character and Substring uses 0.
 
Thaks John, That is actually a good idea!

John Timney (Microsoft MVP) said:
as well as the answers the other guys have provided, you can also import the
visualbasic lib and actually use left, right and mid in c'

--
Regards

John Timney
Microsoft Regional Director
Microsoft MVP
 
A.M said:
Thaks John, That is actually a good idea!

No, it's not. Substring is a perfectly good alternative, and one which
stays within the idiom of C# and .NET, rather than making your code
look like a mixture of C# and old VB.

It will work, but it will be harder for other C# programmers (without
VB experience) to understand. It will require the Microsoft.VisualBasic
assembly to be loaded for no particularly good reason. It will use a
method which has been significantly less thoroughly tested than
Substring (because virtually all .NET programmers use Substring whereas
only a few will be using the ported VB methods).
 
I couldn't agree more. Just because something can be done
is not a logical reason for doing so.


--
<%= Clinton Gallagher
A/E/C Consulting, Web Design, e-Commerce Software Development
Wauwatosa, Milwaukee County, Wisconsin USA
NET csgallagher@ REMOVETHISTEXT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/
 
Thank you for comment.

Unfotunately, I have to be 100% agree with you about about using
Microsoft.VisualBasic assmebly; however I have to subclass my own simple
string class to abstract Left and Mid and Right. It is not the name of
function. It is more convenient to send a simple parameter which is really
needed for that operation if we use it too often.

I was excited because I didn't think about using Microsoft.VisualBasic
assmebly in C# !

Thank you again,
Alan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top