Can't write hyperlink as a string - c#

I want to write something like this
string input = "<form action=\"http://blabla.com\" method=\"post\">...</form>";
but the backslash() where .com ends merge with link... what i can do?
So link becoming http://blabla.com\

This might be more readable with strings that contain both slashes and double quotes. It is for me.
string input = #"<form action=""http://blabla.com"" method=""post"">...</form>";
Also note that a single quote is acceptable in HTML, so this should work too:
string input = #"<form action='http://blabla.com' method='post'>...</form>";
Here's some additional info on literals in C#
http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx

This is only visually inside the Visual Studio Editor. When running, your program should work fine.

Related

read a path from file in a correct syntax with streamreader

I'm completely a junior here. I have tried something like
save a path and file string in a file like:
c:\aaa\bbb\text.txt
then I need to read again as path but I get c:\aaa\bbb\text.txt from streamreader, but I need c:\\\aaa\\\bbb\\\text.txt
Can anyone help me?
I think you might be confusing string literals with a string.
Say I write var myString = "\\" or var myString = #"\", this will show in the debugger as \\, because the debugger will format it as a literal. But if print it to the console, a file, or press the magnifying glass next to the string in the debugger, it will be shown as \, because that is the actual string value. See also verbatim string literal
So, if you do myStreamWriter.Write("c:\\aaa\\bbb\\text.txt");, you will be actually saving the string c:\aaa\bbb\text.txt, and that is also the string that will be read back.
However I fail to understand why you would want three slashes, I can only assume the OP thinks the escaping is done multiple times.

How do I see if a string contains another string with quotes in it?

I am trying to see if a large string contains this line of HTML:
<label ng-class="choiceCaptionClass" class="ng-binding choice-caption">Was this information helpful?</label>
As you can see, this snippet has quotations in multiple places and it's causing problems when I do something like this:
Assert.IsTrue(responseContent.Contains("<label ng-class="choiceCaptionClass" class="ng - binding choice - caption">Was this information helpful?</label>"));
I've tried both of these ways of defining the string:
#"<label ng-class=""choiceCaptionClass"" class=""ng - binding choice - caption"">Was this information helpful?</label>"
and
"<label ng-class=\"choiceCaptionClass\" class=\"ng - binding choice - caption\">Was this information helpful?</label>"
But in each case the Contains() method looks for the literal string with either the double quotes or the backslashes. Is there another way I could define this string so I can correctly search for it?
Escaping the double-quotes with backslashes is the proper thing to do.
The reason your search may be failing is that the strings don't actually match. For example, in your version with backslashes, you have spaces around some of the dashes but your HTML string does not.
Try using regular expressions. I made this one for you but you can test your own regex here.
var regex = new Regex(#"<label\s+ng-class\s*=\s*""choiceCaptionClass""\s+class\s*=\s*""ng-binding choice-caption""\s*>\s*Was this information helpful\?\s*</label>", RegexOptions.IgnoreCase);
Assert.IsTrue(regex.IsMatch(responseContent));
If this is not working use the tester tool to figure it out what part of the pattern is getting off.
Hope this help!

how to make string returned by ResourceManager.GetString not verbatim

Okay, I have a string
string textToShow = "this\nrocks"
which when put in label in winforms window will then show
this
rocks
Which is the result I'd like to get. Now, instead of setting the textToShow in the code, I set it in the resource file. When I tried to get the value from resource file using
Properties.Resources.ResourceManager.GetString("textToShow");
the whole string instead will be treated as verbatim, showing
this\nrocks
when put in a label in a winforms window. This is not the result i'm looking for. What's the best way to store strings with special characters in resource file then? I can do string replace for every special characters, like
string.Replace(#"\n", "\n");
but then I need to replace every special characters whenever I call method ResourceManager.GetString, which I think is not the most elegant solution. If there is some ways to make string returned from method ResourceManager.GetString not verbatim, please do tell me.
Thanks
This was already answered here: StackOverflow: How to deal with newline
Basically you have two useful options:
Use shift + enter in the resource manager text editer to add a new line.
Or use String.Format() to replace {0} with \n on read.
The .Net 4.5 framework has the unescape functionality as shown here:
using System.Text.RegularExpressions;
Regex.Unescape(Properties.Resources.ResourceManager.GetString("textToShow"));
solves your issue. Now you can use \n and \u in the resource files.
On the resource editor type "this<shift+enter>rocks" as the resource value.

Convert string to char

I get from another class string that must be converted to char. It usually contains only one char and that's not a problem. But control chars i receive like '\\n' or '\\t'.
Is there standard methods to convert this to endline or tab char or i need to parse it myself?
edit:
Sorry, parser eat one slash. I receive '\\t'
I assume that you mean that the class that sends you the data is sending you a string like "\n". In that case you have to parse this yourself using:
Char.Parse(returnedChar)
Otherwise you can just cast it to a string like this
(string)returnedChar
New line:
string escapedNewline = #"\\n";
string cleanupNewLine = escapedNewline.Replace(#"\\n", Environment.NewLine);
OR
string cleanupNewLine = escapedNewline.Replace(#"\\n", "\n");
Tab:
string escapedTab = #"\\t";
string cleanupTab= escapedTab.Replace(#"\\t", "\t");
Note the lack of the literal string (i.e. i did not use #"\t" because that will not represent a Tab)
Alternatively you could consider Regular Expressions if you need to replace a range of different string patterns.
You should probably write a utility function to encapsulate the common behaviour above for all the possible Escape Sequences
Then you'd write some Unit Tests to cover each of the cases you can think of.
As you encounter any bugs you add more unit tests to cover those cases.
UPDATE
You could represent a tab in the XML with a special character sequence:
see this article
This article applies to SQL Server but may well be relevant to C# also?
To be absolutely sure, you could try generating a string with a tab in it and putting it into some XML (programmatically) and using XmlSerializer to serialize that to a file to see what the output is, then you can be sure that this will faithfully 'round-trip' the string with the tab still in it.
how about using string.ToCharArray()
You can then add the appropriate logic to process whatever was in the string.
char.parse(string); is used to convert string to char and you can do vice versa
char.tostring();
100% solved

.NET string IndexOf unexpected result

A string variable str contains the following somewhere inside it: se\">
I'm trying to find the beginning of it using:
str.IndexOf("se\\\">")
which returns -1
Why isn't it finding the substring?
Note: due to editing the snippet showed 5x \ for a while, the original had 3 in a row.
Your code is in fact searching for 'se\\">'. When searching for strings including backslashes I usually find it easier to use verbatim strings:
str.IndexOf(#"se\"">")
In this case you also have a quote in the search string, so there is still some escaping, but I personally find it easier to read.
Update: my answer was based on the edit that introduced extra slashes in the parameter to the IndexOf call. Based on current version, I would place my bet on str simply not containing the expected character sequence.
Update 2:
Based on the comments on this answer, it seems to be some confusion regarding the role of the '\' character in the strings. When you inspect a string in the Visual Studio debugger, it will be displayed with escaping characters.
So, if you have a text box and type 'c:\' in it, inspecting the Text property in the debugger will show 'c:\\'. An extra backslash is added for escaping purposes. The actual string content is still 'c:\' (which can be verified by checking the Length property of the string; it will be 3, not 4).
If we take the following string (taken from the comment below)
" '<em
class=\"correct_response\">a
night light</em><br
/><br /><table
width=\"100%\"><tr><td
class=\"right\">Ingrid</td></tr></table>')"
...the \" sequences are simply escaped quotation marks; the backslashes are not part of the string content. So, you are in fact looking for 'se">', not 'se\">'. Either of these will work:
str.IndexOf(#"se"">"); // verbatim string; escape quotation mark by doubling it
str.IndexOf("se\">"); // regular string; escape quotation mark using backslash
This works:
string str = "<case\\\">";
int i = str.IndexOf("se\\\">"); // i = 3
Maybe you're not correctly escaping one of the two strings?
EDIT there's an extra couple of \ in the string you are searching for.
Maybe the str variable does not actually contain the backslash.
It may be just that when you mouse over the variable while debugging, the debugger tooltip will show the escape character.
e.g. If you put a breakpoint after this assignment
string str = "123\"456";
the tooltip will show 123\"456 and not 123"456.
However if you click on the visualize icon, you will get the correct string 123"456
Following code:
public static void RunSnippet()
{
string s = File.ReadAllText (#"D:\txt.txt");
Console.WriteLine (s);
int i = s.IndexOf("se\\\">");
Console.WriteLine (i);
}
Gives following output:
some text before se\"> some text after
17
Seems like working to me...
TextBox2.Text = TextBox1.Text.IndexOf("se\"">")
seems to work in VB.
DoubleQuotes within a string need to be specified like "" Also consider using verbatim strings - So an example would be
var source = #"abdefghise\"">jklmon";
Console.WriteLine(source.IndexOf(#"se\"">")); // returns 8
If you are looking for se\">
then
str.IndexOf(#"se\"">")
is less error-prone. Note the double "" and single \
Edit, after the comment: it seems like the string may contain ecaping itself, in which case in se\"> the \" was an escaped quote, so the literal text is simply se"> and the string to use is Indexof("se\">")

Categories

Resources