From my previous question, Converting chinese character to Unicode, I had a good answer but with some code I didn't understand:
Console.WriteLine("U+{0:x4}", (int)myChar);
Could anyone explain this?
Console.WriteLine("U+{0:x4}", (int)myChar);
is the equivalent to the call:
Console.WriteLine("U+{0}", ((int)myChar).ToString("x4"));
In a format string, the : indicates that the item should be displayed using the provided format. The x4 part indicates that the integer should be printed in its hexadecimal form using 4 characters. Refer to standard numeric format strings for more information.
The 0 indicates which positional argument to substitute. The x displays a hexadecimal number, the 4 has it display four digits.
For example, the character ΘΏ (LATIN SMALL LETTER S WITH SWASH TAIL, codepoint 575) is printed as U+023F since 57510 = 23F16.
That will simply create the literal string "U+1234"... now if you are wanting to convert a unicode code point into a char, you want Convert.ToChar(myChar)
http://msdn.microsoft.com/en-us/library/3hkfdkcx.aspx
Related
i want to format a string. Assume there is:
string unformatedString="004897582515"
string stringFormater="{0:00#-###-###-####}"
After formatting:
string result = String.Format(stringFormater, Int64.Parse(unformatedString));
the result is: 000-044-788-9556
I'd like to know why? Because after parsing unformatedString into Int64 I am getting 4897582515 value as integer, but after formatting it there are always additional zeros(it's based on count of zeros in the beginning of unformatedString).
The simple reason 004897582515 is turning into 000-0xx-xxx-xxx with the format specifier "{0:00#-###-###-####}" is because of the 0's at the beginning
Custom numeric format strings
Format specifiers
0 : Zero placeholder
Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
Maybe you want "{0:###-###-###-####}"
Format specifiers
# : Digit placeholder
Replaces the # symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.
Which in worst case will result in "-xxx-xxx-xxxx"
However, you could hack in a TrimStart('-')
Console.WriteLine(string.Format("{0:###-###-###-####}", 004897582515).TrimStart('-'));
Which will remove any leading dash
Output
489-758-2515
Full Demo Here
Hi I just want to ask about how can i add padding on string.Format so that when I show it , the mask is applied with leading zeros
Heres my c# code
Model.Phone = String.Format("{0:(###) ###-####}", double.Parse(#e.Phone));
Expected result should be
(012) 345-6789
But the results I am getting is
(12) 345-6789
and the leading zero is missing, Hope someone can help me in this problem , Thanks
You would use 000 instead of ###, read more about format in MSDN article Custom Numeric Format Strings
String.Format("{0:(000) ###-####}", double.Parse(#e.Phone));
Format specifier "0"
Replaces the zero with the corresponding digit if one is present;
otherwise, zero appears in the result string.
Format specifier "#"
Replaces the "#" symbol with the corresponding digit if one is
present; otherwise, no digit appears in the result string.
So i have a program that works on text and i need to get the length of string.
BUT if in my word i have a national letter the output of length method is not correct. It gets additional +1 for each national letter, so it returns 6 from "qwerty", but 7 if i use "e with a little tail" instead of regular 'e'.
Any ideas how could i fix that?
Also, sorry for descriptions of letters, but i think stackoverflow takes my national symbols as grammar errors and doesn't allow me to post a question :/
It tells you on the page for string.Length what to do (emphasis mine):
The Length property returns the number of Char objects in this
instance, not the number of Unicode characters. The reason is that a
Unicode character might be represented by more than one Char. Use the
System.Globalization.StringInfo class to work with each Unicode
character instead of each Char.
why do we get the extra text within the char array in c#, looks like a char equivalent but just wondering are there any advantages of having this within the char array, if so where do we use this feature.
That is the integer representation of the char within the ASCII table
They're the ASCII values of what numerical value the characters represent.
http://www.asciitable.com/
This is purely the watch window that is showing you the char byte value and the actual char string vaule.
So, from ASCII Table you can see that 48 is '0' and 49 is '1'
Characters in C# are Unicode, and there are plenty of them. And there are plenty of them that look similar or downright identical or even unreadable. In such cases you need these numbers.
As per the C# Reference, each char from char array shows the Decimal value of it
I came across this line of code today:
int c = (int)'c';
I was not aware you could cast a char to an int. So I tested it out, and found that a=97, b=98, c=99, d=100 etc etc...
Why is 'a' 97? What do those numbers relate to?
Everyone else (so far) has referred to ASCII. That's a very limited view - it works for 'a', but doesn't work for anything with an accent etc - which can very easily be represented by char.
A char is just an unsigned 16-bit integer, which is a UTF-16 code unit. Usually that's equivalent to a Unicode character, but not always - sometimes multiple code units are required for a single full character. See the documentation for System.Char for more details.
The implicit conversion from char to int (you don't need the cast in your code) just converts that 16-bit unsigned integer to a 32-bit signed integer in the natural, non-lossy way - just as if you had a ushort.
Note that every valid character in ASCII has the same value in UTF-16, which is why the two are often confused when the examples are only ones from the ASCII set.
97 is UTF-16 code unit value of letter a.
Basically this number relates to UTF-16 code unit of given character.
These are the ASCII values representing the characters.
They are the decimal representation of their ascii counterpart:
http://www.asciitable.com/index/asciifull.gif
so 'a' would be a 97
They are character codes, commonly known as ASCII values.
Technically, though, the character codes are not ASCII.