No need to use escape seq. for ' in C#? - c#

On MSDN I can read that \' is escape sequence for ' char. However I am able to use it in string without escape sequence like this:
Console.WriteLine("Press 'X' ");
How it is possible?

But how would you write it as a char?
char c = '\'';

char (a single character literal) is a different data type than string (a multi character literal).
In C# a char is declared as:
var c = 'c';
whereas a string is declared as:
var s = "asdf";
As you can see the single quote (') would need to be escaped to declare a char containing the single quote:
var c = '\'';

\' screening is needed for char literals. Reason is that ' can be interpreted as literal boundary character. For strings it is meaningless because there is nothing to confuse with. In strings in turn \" makes sense.

It says that you have to escape ' for a char data type.
char c = '''; // compiler throws error
char c = '\''; // valid

Related

convert a hex string to corresponding emoji string

I'm trying to create a string with emoji "👱" starting from this string "D83DDC71". For doing that I'm trying to convert the string above in this string "\uD83D\uDC71".
If i use this code it work (textbox shows 👱 as expected):
textbox.Text += "\uD83D\uDC71";
but if i use this it doesn't work (textbox shows exact text "\uD83D\uDC71" instead of single character):
textbox.Text += sender.Code.ToString("X").insert(4, #"\u").insert(0, #"\u");
What is the right way to convert hex representation of an emoji to a corresponding C# string (UTF-16)?
Okay. It seems you have a string which gives the hexadecimal of each of the UTF-16 code units of the character U+1F471 (👱).
Since char represents a UTF-16 code unit, split the string into two 4-character chunks, parse that into an int as hexadecimal, cast each to char and then combine them into a string:
var personWithBlondHair = ""
+ (char)int.Parse("D83DDC71".Substring(0, 4), NumberStyles.HexNumber)
+ (char)int.Parse("D83DDC71".Substring(4, 4), NumberStyles.HexNumber);
As per https://dotnetfiddle.net/oTgXfG
You have a string containing two shorts in hexadecimal form, so you need to parse them first. My example uses an overload of Convert.ToInt16 which also accepts an integer specifying the base of the integers in the string which, in our case, is 16 (hexadecimal).
string ParseUnicodeHex(string hex)
{
var sb = new StringBuilder();
for (int i = 0; i < hex.Length; i+=4)
{
string temp = hex.Substring(i, 4);
char character = (char)Convert.ToInt16(temp, 16);
sb.Append(character);
}
return sb.ToString();
}
Please note that this method will fail if the string's length isn't divisible by 4.
The reason this works:
textbox.Text += "\uD83D\uDC71";
is because you've got a string literal containing unicode character escape sequences. When you compile your program, the compiler replaces these escape sequences with the correct unicode bytes. This is why you cannot just add \u in front of the characters during execution to make it work.
Try this one
string str = "D83DDC71";
string emoji = string.Join("", (from Match m in Regex.Matches(str, #"\S{4}")
select (char) int.Parse(m.Value, NumberStyles.HexNumber)).ToArray());
This will Separate your string 4 by 4 into array of strings. then it will convert each of strings into char. Finally it will Join all the chars into one string as emoji. all in one line.

How to concatenate string with a backslash with another string

How can I concatenate the string "\u" with "a string" to get "\u0000"?
My code creates two backslashes:
string a = #"\u" + "0000"; //ends up being "\\\u0000";
The escape sequence \uXXXX is part of the language's syntax and represents a single Unicode character. By contrast, #"\u" and "0000" are two different strings, with a total of six characters. Concatenating them won't magically turn them into a single Unicode escape.
If you're trying to convert a Unicode code point into a single-character string, do this:
char.ConvertFromUtf32(strUnicodeOfMiddleChar).ToString()
BTW, don't use == true; it's redundant.
If I understand you correctly, I think you want to build a single-char string from an arbitrary Unicode value (4 hex digits). So given the string "0000", you want to convert that into the string "\u0000", i.e., a string containing a single character.
I think this is what you want:
string f = "0000"; // Or whatever
int n = int.Parse(f, NumberStyles.AllowHexSpecifier);
string s = ((char) n).ToString();
The resulting string s is "\u0000", which you can then use for your search.
(With corrections suggested by Thomas Levesque.)
the line below creates tow backslash:
string a = #"\u" + "0000"; //a ends up being "\\u0000";
No, it doesn't; the debugger shows "\" as "\", because that's how you write a backslash in C# (when you don't prefix the string with #). If you print that string, you will see \u0000, not \\u0000.
Nope, that string really has single backslash in. Print it out to the console and you'll see that.
Escape your characters correctly!!
Both:
// I am an escaped '\'.
string a = "\\u" + "0000";
And:
// I am a literal string.
string a = #"\u" + "0000";
Will work just fine. But, and I am going out on a limb here, I am guessing that you are trying to escape a Unicode Character and Hex value so, to do that, you need:
// I am an escaped Unicode Sequence with a Hex value.
char a = '\uxxxx';

C# Weird Backslash on Convert.ToChar()

I'm trying to convert a xml character entity to a C# char...
string charString = "₁".Replace("&#", "\\").Replace(";", "");
char c = Convert.ToChar(charString);
I have no idea why it is failing on the Convert.Char line. Even though the debugger shows charString as "\\\\x2081" it really is "\x2081", which is a valid Unicode character. The exception is too many characters.
The documentation for ToChar(string) is quite readable:
Converts the first character of a specified string to a Unicode character.
Also:
FormatException – The length of value is not 1.
It will not convert a hex representation of your character into said character. It will take a one-character string and give you that character back. The same as doing s[0].
What you want is:
string hex = "₁".Replace("&#x", "").Replace(";", "");
char c = (char)Convert.ToInt32(hex, 16);
Convert.ToChar(value) with value is a string of length 1. But charString is "\\x2081" length over 1.
Seems "₁" is Unicode Hex Character Code (Unicode Hex Character Code ₁ ). So you must do that:
string charString = "₁".Replace("&#x", "").Replace(";", "");
char c = (char)Convert.ToInt32(charString , NumberStyles.HexNumber);
Note: It's HTML Entity (hex) of SUBSCRIPT ONE (see in link above ^_^)

Convert hexadecimal unicode character into its visual representation

I am trying to make a C# program that translates unicode character from its hexadecimal format to a single character, and I have a problem. This is my code:
This works:
char e = Convert.ToChar("\u0066");
However, this doesn't work:
Console.WriteLine("enter unicode format character (for example \\u0066)");
string s = Console.ReadLine();
Console.WriteLine("you entered (for example f)");
char c = Convert.ToChar(s);
Because (Convert.ToChar("\\u0066")) gives the error:
String must be exactly one character long
Anyone have an idea how to do this?
int.Parse doesn't like the "\u" prefix, but if you validate first to ensure that it's there, you can use
char c = (char)int.Parse(s.Substring(2), NumberStyles.HexNumber);
This strips the first two characters from the input string and parses the remaining text.
In order to ensure that the sequence is a valid one, try this:
Regex reg = new Regex(#"^\\u([0-9A-Fa-f]{4})$");
if( reg.IsMatch(s) )
{
char c = (char)int.Parse(s.Substring(2), NumberStyles.HexNumber);
}
else
{
// Error
}
Convert.ToChar("\u0066");
This is a one-character string at run-time, because the compiler processed the backslash sequence.
The rest of your code is dealing with six character strings { '\\', 'u', '0', '0', '6', '6' }, which Convert.ToChar cannot handle.
Try char.Parse (or possibly Int16.Parse(s, NumberStyles.AllowHexSpecifier) followed by a cast to char).

How to get a variable of type char to have a value of ' in c#

A bit of a dumb one... but how do I get a variable of type char to have a value of '?
e. g.
char c = ''';
char a = 'A';
char b = 'B';
char c = '\'';
the backslash is called an escape character.
and if you want a backslash it's
char c = '\\';
Here's some more for good measure:
\t - tab
\n - new line
\uXXXX - where XXXX is the hexadecimal value of a unicode character
char a=65 means 'A' in c++. don't know whether it will work in c#
To complete you answer:
in C#, the following statement won't compile, because you're trying to put an Int32 into a Char (no implicit casting exists)
char a = 65;
To convert ASCII codes to a character, you have to use the static Convert class in C#:
char a = Convert.ToChar(65); // where 65 is the ascii

Categories

Resources