"OutdoorGuy" <Ou********@fishing.com> wrote in message
news:uT**************@TK2MSFTNGP02.phx.gbl...
Greetings,
I have a "newbie" question. I was wondering if there is anything in C#
that corresponds to VB's "Left" function? I simply want to retrieve the
leftmost characters of a string and I wasn't sure what the best way was
to do that.
Thanks in advance!
*** Sent via Developersdex http://www.developersdex.com ***
In addition to the other replies, and using their substring answer to create
a utility method that performs the Left (although you can use the
VisualBasic reference and import it...)
public string Left(string Original, int Count)
{
// Can't remember if the Left function throws an exception in this case,
but for
// this method, we will just return the original string.
if (Original == null || Original == string.Empty || Original.Length <
Count) {
return Original;
} else {
// Return a sub-string of the original string, starting at index 0.
return Original.Substring(0, Count);
}
}
public string Right(string Original, int Count)
{
// same thing as above.
if (Original == null || Original == string.Empty || Original.Length <
Count) {
return Original;
} else {
// blah blah blah
return Original.Substring(Original.Length - Count);
}
}
btw, this is untested code :)
HTH,
Mythran