By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,865 Members | 1,453 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,865 IT Pros & Developers. It's quick & easy.

Trying to convert string "TextText" to "Text

P: n/a
Hi

I am trying to do an ldap lookup. I can pick up the domain name in the
standard format DOMAIN\USERNAME. The problem is the company I work for
expects the query in the format DOMAIN:USERNAME.
I tried to do a simple string replace to replace \ to : but it turns
out the only string you can't replace in c# is \ as it gives the
error "Invalid Escape Sequence" - using "\\" or @"\" doesnt work
either

// doesnt work
string result = Regex.Replace(string.Format("{0}", b), "\", ":");
Next step I tried was to convert the string to unicode and replace the
unicode \ with a unicode :

This works in terms of outputting to a label but does anyone know how
to convert a unicode sequence back to string values

Expand|Select|Wrap|Line Numbers
  1. string u1 = username.Text;
  2.  
  3. UnicodeEncoding unicode = new UnicodeEncoding();
  4. Byte[] encodedBytes = unicode.GetBytes(u1);
  5. foreach (Byte b in encodedBytes)
  6. {
  7. string result = Regex.Replace(string.Format("{0}", b), "92", "58");
  8. // as output to a label to test result
  9. l1.Text += string.Format("{0}", result);
  10. }
  11.  
This code will output a unicode sequence to a label but its not very
useful. What I need to do is encode the unicode back to string and
pass that to my ldap code to perform the query (which works fine if I
hard code it).

What I tried to do was to create another Byte[] called decodedBytes
and add each parsed token from the first Byte[] and decode this when
complete.
Expand|Select|Wrap|Line Numbers
  1. String decodedString = unicode.GetString(decodedBytes);
  2.  
Does anyone know an alternative way to convert \ to : or does anyone
know how to convert a unicode sequence back to string values.

Thanks

Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Both these string.Replace method overloads work:

string loginName = @"DOMAIN\USERNAME";
Console.WriteLine(loginName.Replace("\\", ":"));

Console.WriteLine(loginName.Replace('\\', ':'));
--
Steve Willcock (MCSD for Microsoft.NET)
http://www.willcockconsulting.com/

"Joebloggs" <jo***********@hotmail-dot-com.no-spam.invalid> wrote in message
news:41********@Usenet.com...
Hi

I am trying to do an ldap lookup. I can pick up the domain name in the
standard format DOMAIN\USERNAME. The problem is the company I work for
expects the query in the format DOMAIN:USERNAME.
I tried to do a simple string replace to replace \ to : but it turns
out the only string you can't replace in c# is \ as it gives the
error "Invalid Escape Sequence" - using "\\" or @"\" doesnt work
either

// doesnt work
string result = Regex.Replace(string.Format("{0}", b), "\", ":");
Next step I tried was to convert the string to unicode and replace the
unicode \ with a unicode :

This works in terms of outputting to a label but does anyone know how
to convert a unicode sequence back to string values

Expand|Select|Wrap|Line Numbers
  1.  string u1 = username.Text;
  2.  UnicodeEncoding unicode = new UnicodeEncoding();
  3.  Byte[] encodedBytes = unicode.GetBytes(u1);
  4.  foreach (Byte b in encodedBytes)
  5.  {
  6.  string result = Regex.Replace(string.Format("{0}", b), "92", "58");
  7.  // as output to a label to test result
  8.  l1.Text += string.Format("{0}", result);
  9.  }
  10.  

This code will output a unicode sequence to a label but its not very
useful. What I need to do is encode the unicode back to string and
pass that to my ldap code to perform the query (which works fine if I
hard code it).

What I tried to do was to create another Byte[] called decodedBytes
and add each parsed token from the first Byte[] and decode this when
complete.
Expand|Select|Wrap|Line Numbers
  1.  String decodedString = unicode.GetString(decodedBytes);
  2.  

Does anyone know an alternative way to convert \ to : or does anyone
know how to convert a unicode sequence back to string values.

Thanks

Nov 18 '05 #2

P: n/a
Thanks Steve

I just discovered that myself!
One of those beginer errors!


string u1 = username.Text;
u1 = u1.Replace('\\', ':');
Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.