1

Beginning in c#, haven't seen a duplicate. What I want to do is:

this string: İntersport transform to this string: \u0130ntersport

I found a way to convert everything in unicode but not to convert only the special character.

thanks in advance for your help

edit:

I have tried your solution:

 string source = matchedWebIDDest.name;
 string parsedNameUnicode = string.Concat(source.Select(c => c < 32 || c > 255 ? "\\u" + ((int)c).ToString("x4") : c.ToString()));

But I get : "System.Linq.Enumerable+WhereSelectEnumerableIterator`2[Syst‌​em.Char,System.Strin‌​g]"

François Richard
  • 6,817
  • 10
  • 43
  • 78
  • You could iterate over each char and use the solution provided here: http://stackoverflow.com/questions/13291339/convert-string-to-unicode-representation to convert the character and then build a new string. – Joakim Skoog May 11 '17 at 14:22
  • I saw this but it would convert all my character .. and not only those I need to convert – François Richard May 11 '17 at 14:24
  • I've copied+pasted your code, changed `matchedWebIDDest.name` to `"İntersport"`, added `Console.Write(parsedNameUnicode);` and I've seen `\u0130ntersport` outcome – Dmitry Bychenko May 11 '17 at 14:51
  • this is weird it's working with you no Linq solution, I'm wondering why ... Thanks a lot for your help and patience – François Richard May 11 '17 at 14:53
  • It's not clear what you mean by "special character." İ is a letter in at least one language's alphabet. Letters of an alphabet are not normally termed "special." What is the larger purpose in changing the representation of the text in a string (to a format that can be stored in a C# source file that doesn't use the default encoding of UTF-8)? – Tom Blodget May 11 '17 at 16:53

1 Answers1

6

You can try using Linq:

  using System.Linq;

  ...

  string source = "İntersport";

  // you may want to change 255 into 127 if you want standard ASCII table
  string target = string.Concat(source
    .Select(c => c < 32 || c > 255  
       ? "\\u" + ((int)c).ToString("x4") // special symbol: command one or above Ascii 
       : c.ToString()));                 // within ascii table [32..255]

  // \u0130ntersport
  Console.Write(target);

Edit: No Linq solution:

  string source = "İntersport";

  StringBuilder sb = new StringBuilder();

  foreach (char c in source) 
    if (c < 32 || c > 255)
      sb.Append("\\u" + ((int)c).ToString("x4"));
    else
      sb.Append(c);

  string target = sb.ToString();
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215