473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Trying to convert string "TextText" to "Text

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
2 2237
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
Thanks Steve

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


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Max | last post by:
Hi, I am trying to convert string entered in a field to uppercase using its formula property. I know it can be done using trigger but still I want to use formula property to achieve the same. ...
2
by: Nathan | last post by:
Is there a way to convert a string to a CipherMessage? I am calling a function that decrypts a CipherMessage and returns the value. The only problem is when I want to use an encrypted value stored...
3
by: news.hku.hk | last post by:
could you tell me how can i convet a string to integer?? #include <iostream> #include <string> using namespace std; int main(){ int integer; string buffer("123456789");
3
by: Petr Jakes | last post by:
Hi, I am trying to convert string to the "escaped string". example: from "0xf" I need "\0xf" I am able to do it like: a="0xf" escaped_a=("\%s" % a ).decode("string_escape") But it looks a...
5
by: XML newbie: Urgent pls help! | last post by:
function to convert string to 1 dimensional array of long in VB.Net
3
by: Ursula | last post by:
Is it possible to convert a string in a file. The problem is this: I have an object string that is a file xml and I want to pass to Deserialize function, but Deserialize function expect an object...
3
by: Shawn Ferguson | last post by:
Hello All, I'm trying to do what I would think would be simple and straightforward, but it is not. I have a 2 textbox on a form, a label, and a button, when I click the botton I want to add the...
4
by: Man4ish | last post by:
HI , I am trying to convert string into char array of characters.but facing problem. #include <iostream> #include <string> using namespace std; int main() { string t="1,2,3,4,5,6";
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
9
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.