Chop Off LDAP:// From DN

T

Travis Parks

Hello:

Really quick, is there a built-in method for chopping off the LDAP://{hostname}/
piece from the DN using code in the System.DirectoryServices
namespace? For now, I am just doing a substring, but I don't want to
overlook using a built-in method.

Thanks,
Travis Parks
 
J

Jeff Johnson

Inasmuch as the "ldap:" is a "scheme" in the URI sense of the word, you
might prefer using the URI class to parse the elements of the string, so
that you can ignore the scheme. It would be more flexible, in that
differently-formed strings might be handled better.

But really, if you know the string will always look like that, I don't
think it gets much simpler or easier to understand than just calling
Substring(). :)

Ehh, depends on your point of view. I think a Replace() would be the tiniest
bit clearer to someone looking at the code later, since you'll see what
string is being replaced and that it's being replaced with the empty string,
and you'll think "Oh, he's blowing away the LDAP:// from the string,"
whereas with Substring() you have to devote a whole second or so to looking
at the starting point and length to see what's being removed. Think of what
you could do with all those extras seconds...!
 
T

Travis Parks

Peter said:
[...] with Substring() you have to devote a whole
second or so to looking at the starting point and length to see what's
being removed. Think of what you could do with all those extras
seconds...!
It's such a minor point, I think we can agree any number of solutions
are probably suitable.  :)
But, if documenting the statement is in question, a code comment can
address that (even though I agree self-documenting code is better).  And
the Replace() method is less efficient and less precise than Substring().

Oh, and note that in terms of self-documenting:

   hostName = hostName.Substring("LDAP://".Length);

…is probably pretty clear.  :)

Pete

Part of the challenge, too, is that I must chop off the hostname:port
from the DN. So, I can't know the full text anyway (unless I rebuild
it). Searching for the last '/' and doing a substring is easier, IMO.
 
T

Travis Parks

If you need to remove more than just the fixed-length scheme, then using
the System.Uri class (as I suggested earlier) or the Regex class (which
provides robust pattern-matching) are possibly other interesting options
than just straight string manipulation.

Whether in your case they would be preferred, I don't know.  If you are
sure you can just scan for the next single forward slash and trim the
string there, then that's pretty straight-forward and should work fine.
  But if there are concerns about inputs that don't match that exact
pattern, for which you need more elaborate handling, these other options
might be more suitable.

Pete

I have been concerned about there being slashes later on in the text.
 

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

Top