Not able use superscript in C# [duplicate] - c#

This question already has answers here:
How to insert a Symbol (Pound, Euro, Copyright) into a Textbox
(2 answers)
Closed 2 years ago.
I want to display the String with superscript like Shibu® using C#.
Unicode of ®:- U+000AE
Here is the code:-
String s = "Shibu";
Console.write(s.join("\xBU+000AE", s));
I am not getting proper output like Shibu®.

You need to concatenate those strings, string.Join is for joining several strings together with a special one repeated in between.
Also, representation of unicode characters is done with \uXXXX where XXXX is the hexadecimal code point value.
string s = "Shibu";
Console.WriteLine(s + "\u00AE");
Or simply
string s = "Shibu\u00AE";
Console.WriteLine(s);
Also, you can directly write unicode characters; C# strings are unicode.
string s = "Shibu®";
Console.WriteLine(s);
This does not, however, set the character as "superscript" in the unicode or font meaning of term. I'm not sure that is possible with native C# strings, you need to cope with the existing basic unicode characters.
For fancier, use a visual rich textbox control, or a WPF control, that allow you to set font options.

Related

Literal to string [duplicate]

This question already has answers here:
Can I expand a string that contains C# literal expressions at runtime
(5 answers)
Closed 6 years ago.
I have a literal as a string in a file
def s_CalculatePartiallyUsedTechPenalty(rate):\n total = min(rate,0)\n title = \"Partially Used Technology Penalty\" \n return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)
when reading the file the text obviously looks like this:
def s_CalculatePartiallyUsedTechPenalty(rate):\\n total = min(rate,0)\\n title = \\\"Partially Used Technology Penalty\\\" \\n return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)
Is there clean way to convert this string so that the value in the file is also the actual value of the string in code. In other words that that \n for example is \n and not \\n.
I understand that I can write a method that goes and replaces all the applicable values, but I do not want to do that unless it is the only way.
Edit: In response to John Wu's answer. No I am not confused. I do understand exactly that this is happening however I want to convert the literal value "\n" to the newline character. So instead of the literal value of \n it should be a new line.
Basically the inverse of How to convert a string containing escape characters to a string
You are confusing yourself. The string held in the file will be exactly the same as the string held in a string variable obtained by reading the file.
Perhaps you are using Visual Studio to inspect the string (i.e. using the Watch window or just hovering over the variable while the code is in debug mode). In this case, Visual Studio will display the extra slash to indicate that the string variable contains the literal value "\n" instead of a newline character.
If you want to eliminate the escape characters in the Watch window, you can append the format specifier ,nq to the variable name (link).
See also this question on StackOverflow.
If you can not fix file-writing code, that you can solve this issues in a following way:
String.Replace(#"\\\", #"\");
String.Replace(#"\\", #"\");
Or, in case, if you normal unescaped string,
String.Replace(#"\\\""", "\"");
String.Replace(#"\\n", Environment.NewLine);
P.s. Also think about other special characters, like \t
UPDATED:
Even better approach:
class Program
{
static void Main(string[] args)
{
var escaped = #"def s_CalculatePartiallyUsedTechPenalty(rate):\n total = min(rate,0)\n title = \""Partially Used Technology Penalty\"" \n return RateItem(title,total,FinancialUniqueCode.PartiallyUsedTechPenalty,False)";
var unescaped = Regex.Unescape(escaped);
Console.WriteLine(unescaped);
}
}

console.writeline cannot display Chinese characters [duplicate]

This question already has answers here:
How to write Unicode characters to the console?
(5 answers)
Closed 6 years ago.
Background:
I have a table in SQL database, two of the columns are English names and Simplified Chinese names.
With C#, I had records of this table displayed as buttons with both names, such as: Car车. This is how I did it:
button.Text = x.EnglishName + x.ChineseName; The buttons displays correctly.
3.I would like to compare button.Text to other strings, like so:
for (int K = 0; K < alist.Count; K++)
{
string alpha = alist.[K];
if (alpha == button.Text)
//blahblahblah
}
Problem:
There is always an error.
And I found out why: when I use Console.Writeline(button.Text), the output is Car?.
Each Chinese character is turned into a "?"
So, apparently, writing Chinese characters onto the face of a button is fine. But when reading Chinese characters off the face of a button does not work.
How do I correct this?
You might need to change the Encoding type -
Console.OutputEncoding = Encoding.Unicode // For UTF-16
See here for other encoding types available.

How to write symbol in c# that represents the gender masculine

I wan't to concatenate some id's in C# code to export to a specific data base. The problem is that data base use a specific symbol to concatenate the id's. The symbol used is like the symbol for the gender masculine (something like this: ♂). If I try to copy here I only get the '0' character. I also try to find his ascii code but I couldn't find it. I get the symbol by exporting data from file maker pro data base.
What I want is to create a array of id's concatenated by this strange symbol in C#. For example: 12[symbol]123[symbol]
Remember, ascii is what we used in the 1970's. You want the Unicode codepoint, not the ascii code. If you don't understand the difference then stop everything you are doing and read this before you write any more code:
http://www.joelonsoftware.com/articles/Unicode.html
The Mars symbol is the Unicode codepoint u2642, so in C# that would be
string mars = "\u2642";
When you have obtained the symbol in some way, just paste it into your source code:
string m = "♂";
The symbol a little different that this: ♂. The circle is smaller and the arrow is bigger
Although it's not impossible that there are 2 variations of a symbol in the Unicode space, the difference in appearance is probably due to different Fonts.
Still don't known the code for the symbol
As several people posted here, the code is 2642 and the C# notation is "Male(\u2642)" or simply type/paste "Male(♂)"

Which passwordchar shows a black dot (•) in a winforms textbox using code? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Which passwordchar shows a black dot (•) in a winforms textbox?
Unicode encoding for string literals in C++11
I want to use code to reveal the password or make it a dot like •
textBoxNewPassword.PasswordChar = (char)0149;
How can I achieve this?
http://blog.billsdon.com/2011/04/dot-password-character-c/ suggests '\u25CF';
Or try copy pasting this •
(not exactly an answer to your question, but still)
You can also use the UseSystemPasswordChar property to select the default password character of the system:
textBoxNewPassword.UseSystemPasswordChar = true;
Often mapped to the dot, and always creating a consistent user experience.
You need to look into using the PasswordBox control and setting the PasswordChar as *.
Example:
textBox1.PasswordChar = '*'; // Set a text box for password input
Wikipedia has a table of similar symbols.
In C#, to make a char literal corresponding to U+2022 (for example) use '\u2022'. (It's also fine to cast an integer literal as you do in your question, (char)8226)
Late addition. The reason why your original approach was unsuccessful, is that the value 149 you had is not a Unicode code point. Instead it comes from Windows-1252, and Windows-1252 is not a subset of Unicode. In Unicode, decimal 149 means the C1 control code "Message Waiting".
You could translate from Windows-1252 with:
textBoxNewPassword.PasswordChar =
Encoding.GetEncoding("Windows-1252").GetString(new byte[] { 149, })[0];
but it is easier to use the Unicode value directly of course.
In newer versions of .NET, you need to call:
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
before you can use something like Encoding.GetEncoding("Windows-1252").
textBoxNewPassword.PasswordChar = '\u25CF';

How do you write a tab (\t) inside a "#..." C# string? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# #“” how do i insert a tab?
I'm trying to just use the tab on my keyboard but the compiler interprets the tabs as spaces. Using \t won't work either, it will interpret it as \t literally. Is it not possible or am I missing something?
string str = #"\thi";
MessageBox.Show(str); // Shows "\thi"
Split your string and insert a \t where you want it?
var str = #"This is a" + "\t" + #"tab";
The whole point of a verbatim string literal is that escaping is turned off such that backslashes can be read as they are written. If you want escaping, then use a regular string literal (without the at symbol).
You could, of course, put a literal tab character (by pressing the tab key) within the string.
Another option is to specify the tab as a parameter in string.Format:
string.Format(#"XX{0}XX", "\t"); // yields "XX XX"
in VS2010 - if you copy to clipboard from a richtext application, and that content has a tab in it, I believe it will paste into the VS2010 editor as such.
(i consider this on the side of buggy and wouldn't be surprised if the behavior changes in the future)
You are using a string literal.
Instead, just do this:
string str = "\thi";
MessageBox.Show(str);

Categories