Hello,
To answer you, the function takes an string and returns what it says
after its last "\".
It can be replaced with this equivalent potentially-more-clear less-
memory-expensive code:
public static string ExtractUserName(string path)
{
//Adding 1 to avoid returning the "\" and also to avoid an
exception if the string doesn't contain any "\".
return path.Substring(path.LastIndexOf('\\') + 1);
}
Take care of not passing null, on either version, because it will
throw an exception in that situation.
I recomend to add the following code at the begin of the method unless
you want / expect the exception I mentioned above:
if (String.IsNullOrEmpty(path))
{
return String.Empty;
}
Another option is to surround the code with a try, which in .NET
generated a faster code, as try is inexpensive in .NET (this is not
true in others platforms such as Java), but programming for the
exception is usually harder to undestand.
Also make '\\' a constant.
And for such weird* question, can you please add a please next time?
thanks.
*Why this question is weird: because it makes me feel like if I were
on an exam, and not like I were helping or solving a problem. Perhaps
more context would help too.
PD: can anybody tell me how a path comes to give an user name? which
is what I can tell from the naming of the method.
Cheers!
Al J. Ramos
On 22 feb, 11:01, Learner <[email protected]> wrote:
> Hi,
>
> Can some one explain the below function
>
> public static string ExtractUserName(string path)
> {
> string[] userPath = path.Split(new char[] { '\\' });
> return userPath[userPath.Length - 1];
> }
>
> Thanks,
>
> L