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

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);

Related

Not able use superscript in C# [duplicate]

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.

Why is this regex replace statement failing to do anything? [duplicate]

This question already has answers here:
What is a word boundary in regex?
(13 answers)
Closed 3 years ago.
Below is a tiny method to basically replace the word "Stack" with ".png" in a string. So something called "Grid01Stack" would return as "Grid01.png" however the operation doesn't do anything at all, the string remains the same. What is going wrong? Here is the code:
private string GetUriFromName(string GridName)
{
string result = Regex.Replace(GridName, #"\bStack\b", ".png");
return (#"Resources/Images/"+result);
}
While you can simply do, per Yuri and Cid's suggestions:
GridName.Replace("Stack",".png")
This is not the best option if the word 'Stack' will ever appear more than once in the string, as it will replace all instances. So, for example, "Stack01Stack" will become ".png01.png". As you are trying to form a good filename, you really only want to replace the last occurence of "Stack" with ".png", and only if it at the end of the string. Therefore, using "Stack\b" as the comments suggested could end up messing with valid filenames as well, if Stack shows up multiple times. For instance, using that Regex "GridStack-01Stack" will become "Grid.png-01.png"
This is all based on speculation of what these strings might be, so this solution might not be necessary, but I'd recommend the following Regex, which will only change the word Stack if it occurs at the end of the string:
string result = Regex.Replace(GridName, "Stack$", ".png");
\bStack**\b** - is looking for the whole word 'Stack' with spaces, tab, line break, etc before and after word.
You just need the String.Replace for you case.
• String replace: string x = "Grid01Stack".Replace("Stack", ".png");
• Regex: string x = Regex.Replace("Grid01Stack", "[Ss]tack$", ".png");
The regex will search for Stack or stack which are always in the end of the string.

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);
}
}

Appended extra Escape character in string in c# [duplicate]

var phone = #"^\+(?:[0-9] ?){6,14}[0-9]$";
phone will then equal ^\\+(?:[0-9] ?){6,14}[0-9]$
I thought (and the examples I found seem to show) the # character meant to leave my string how I have it. Why is it doubling the \ and how do I stop it?
The visual studio debugger will show it as if it were doubled, since in C# a \ would precede an escape sequence. Don't worry - your string is unchanged.
It only looks like it's doubled in the debug inspectors.
Note that the strings shown in the inspectors don't start with # - they are showing how you would have to write the string if you were to do it without the #. The two forms are equivalent.
If you're really worried about the contents of the string, output it in a console app.
To reiterate in another way, the comparison
var equal = #"^\+(?:[0-9] ?){6,14}[0-9]$" == "^\\+(?:[0-9] ?){6,14}[0-9]$"
will always be true. As would,
var equal = #"\" == "\\";
If you examine the variables using the Text Visualizer, you will be shown the plain unescaped string, as it was when you declared it verbatim.

Getting NewLine character in FreeTextBox control in Asp.net

I am using FreeTextBox control
in asp.net.When I am getting its HtmlStrippedText in my code I am getting the String without HTML tags.Now how can I get the new line character from this String i.e. I want to Replace All the NewLine characters with Special Symbol "#".
Got the Solution:
Got the HtmlStrippedText in String str and then got replace it like this:
char enter=(char)111;
temp= str.Replace(enter+"", "\n");
If it is anything like the base ASP:TextBox, you can just grab the string from the text property and do something like
var test = txtYourControl.Text.Replace(Environment.NewLine, "#");
This will vary between browsers (perhaps even between the operating systems on which the browser is running). More on newline definitions here.
I have found the most reliable option to be to search and replace "\n" rather than Environment.NewLine which is "\r\n" in a Windows environment.
string text = this.txtField.Text.Replace("\n", "#");
HtmlStrippedText is used to get the plain text
You should use the text property of freetextbox control
u can remove new line using this code.
lblTitle.Text = txtFreetextbox.Text.Replace("<br>","#");
Click this link
http://freetextbox.com/docs/ftb3/html/P_FreeTextBoxControls_FreeTextBox_Text.htm

Categories