THANK YOU to all of you for taking time to explain my question. I
was not clear about the new char[]{'\\'} and it makes sense now.

Ramos - First off sorry for not using the word PLEASE. I would use a
lot of these before in the beginning but what I understand from the
forums online is that asking a question clearly is more/equally
important. But I should have said PLEASE and I apologize for not using
it.

On Feb 23, 12:00 am, Theraot <[email protected]> wrote:
> 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

Reply via email to