I'm trying to read through a file and look for this tag
,<table name="File">,
i've read a bunch and everyone tells me to use #""" or \" to accept the double quotes in the middle of the string, but what it does is turns my string into this. <table name=\"File\"> and it still doesn't work because thats not how it is in the file. examples:
string tblName = " <table name="+#"""File"""+">";
string tblName = " <table name=\"File\">";
Nothing seems to work. It just addes the \ into the string and i can't replace it because it removes the quotes. Any suggestions?
Thanks
string tblName = " <table name="+#"""File"""+">";
try
{
// Only get files that begin with the letter "c."
string[] dirs = Directory.GetFiles(#"C:\Users\dylan.stewart\Desktop\Testing\", "*.ism");
//Console.WriteLine("The number of files starting with c is {0}.", dirs.Length);
foreach (string dir in dirs)
{
foreach( string line in File.ReadLines(dir))
if(line.Contains(tblName))
{
Console.WriteLine(dir);
}
//Console.WriteLine(dir);
}
}
The above methods for adding " into a string are correct. The issue with my OP is i was searching for a specific amount of white space before the tag. I removed the spaces and used the mentioned methods and it is now working properly. Thanks for the help!
string tblName = "<table name=" + '"' + "File" + '"' + ">";
should work since the plus sign concatenate
It should be either
string tblName = #" <table name=""File"">";
or
string tblName = " <table name=\"File\">";
No need for concatenation. Also what do you mean "it still doesn't work"? Just try Console.Write() and you'll see it ok. If you mean the backslashes are visible while inspecting in debugger then it's supposed to be that way
B
Related
Morning folks,
I have an ASP.Net C# page that pulls in a list of servers from a SQL box and displays the list of servers in a label. ("srv1,srv2,srv3"). I need to add double quotes around each of the servers names. ("srv1","srv2","srv3",)
Any help would be greatly appreached.
If you have string
string str = "srv1,srv2,srv3";
Then you can simply do
str = "\"" + str.Replace(",", "\",\"") + "\"";
Now str contains "srv1","srv2","srv3"
As far as I can understand, you are trying to use double quotes in a string.
If you want to use such,
you can use escape character:
("\"srv1\",\"srv2\",\"srv3\"",)
for the sake of simplicity, you can even convert it to a function:
private string quoteString(string serverName){
return "\"" + serverName + "\"";
}
Also, if you have already "srv1,srv2,srv3" format, find ',' characters in the string and add " before and after comma. Also, notice that you should add to first index and last index ".
Hi I have the following line:
var table = #"<table id=""table_id"" class=""display"">
which is building a table and continues on the next line but I'm just trying to append a string at the end of table_id :
var table = #"<table id=""table_id" + instance + """ class=""display"">
so the final output (if instance = 1234) should be:
<table id="table_id1234" class="display">
But I think the quotes are throwing it off. Any suggestions on how t achieve the last line?
Thanks
A string.Format method placeholder is enough to concatenate instance without cutting through quote signs ({0} is the placeholder):
var table = string.Format(#"<table id=""table_id{0}"" class=""display"">", instance);
Or you can use escape sequence \" for escaping quotes without string literal:
var table = "<table id=\"table_id" + instance + "\" class=\"display\">"
Result:
<table id="table_id1234" class="display">
Demo: .NET Fiddle
Try to use escape character for double quote(\") using this code:
var id = "1234";
var table = "<table id=\"table_id" + id + "\" class=\"display\">";
Here is an online tool for converting string to escape/unescape:
https://www.freeformatter.com/java-dotnet-escape.html
So you can copy the result and place your variables.
I think the best idea and newest idea for this situation is $ sign before your text and with this sign you dont need to extra sign in your string
example
vat table = $"<table id='table_id{instance}' class='display'">
# is used to escape double quotes from one string but in your example, you are actually concatenating three different strings, soyou should escape the third string as well like this:
var table = #"<table id=""table_id" + instance + #" "" class=""display"" >";
Alternatively, you could also use the StringBuilder class which is more memory efficient and might make your strings easier to read.
I think I can't see the forest for the trees here. I want to write a string. Using Linq to SQL I have created a result and I'm looping through it to dynamically write anchor tags.
But, the code is producing this:
<a 45="" href="ADappointment.aspx?openingid">My person booked< /a >
I want:
<a href="ADappointment.aspx?openingid=45">My person booked< /a >
Here's what I'm doing:
foreach (var anOpening in results)
string sFlag = #"";
sFlag = #"<td>" + patient.FirstName + " " + patient.LastName + " accepted </td>";
...
What am I doing wrong?
You have quotes in your href value the browser doesnt expect.. so it is rendering it completely incorrectly.
You are producing this:
href="urlhere.aspx?id="99""
Note the quotes around the ID. Remove those from your code. You want something like this:
sFlag = #"<td><a href=""ADappointment.aspx?openingid=" + anOpening.OpeningId + """>" + /* the rest here */
Ideally you would use a library to do this. There is a TagBuilder class in the MVC assembly.
Use String.Format to clearly format string.
foreach (var anOpening in results)
{
var sFlag = String.Format(#"<td>{1} {2} accepted </td>", anOpening.OpeningId, patient.FirstName, patient.LastName);
}
I need to put an adress into a appointment. The address is constructed out of several variables. Of course I also need some newlines. But "\n" doesnt result in an new line when i open the appointment in outlook.
Ok here is code snippet:
string address = name + "\n" + strasse + "\n" + plz.ToString() + " " + ort;
if ( telefon != "") {
address = address + "\nTelefon:: " + telefon;
}
if ( natel != "") {
address = address + "\nNatel: " + natel;
}
if ( mail != "") {
address = address + "\nE-Mail: " +mail;
}
Nothing special. The Problem is when i write this to the body of an appointment, then there aren't any actual newlines.
Its pretty hard to diagnose this without seeing at least an example of the string you are passing, but one thing that I tend to do in my C# code is to use the constant:
Environment.NewLine
Or I use the StringBuilder class with the AppendLine() call to add a newline.
Edit: Based on your code snippet, I would write it this way (it will be more performant as well). With your snippet, lots of strings are being allocated (because strings are immutable). The recommended approach in this case is to use StringBuilder.
StringBuilder address = new StringBuilder();
address.AppendLine(name);
address.AppendLine(strasse);
address.Append(plz.ToString()); // This may not be neccessary depending on the type of plz, StringBuilder has overloads that will convert base types to string for you
address.Append(" ");
address.Append(ort);
if (!string.IsNullOrEmpty(telefon))
{
address.AppendLine();
address.Append("Telefon:: ");
address.Append(telefon);
}
if (!string.IsNullOfEmpty(natel))
{
address.AppendLine();
address.Append("Natel: ");
address.Append(natel);
}
if (!string.IsNullOrEmpty(mail))
{
address.AppendLine();
address.Append("E-Mail: ");
address.Append(mail);
}
return address.ToString();
Note: If you are using .Net 4.0 you can use string.IsNullOrWhitespace instead of IsNullOrEmpty to check for not just an empty string, but one that contains only whitespace.
Edit 2 - Based on your answer of needing <br /> tags instead of newlines.
const string newLine = " <br /> ";
StringBuilder address = new StringBuilder();
address.Append(name);
address.Append(newLine);
address.Append(strasse);
address.Append(newLine);
address.Append(plz.ToString()); // This may not be neccessary depending on the type of plz, StringBuilder has overloads that will convert base types to string for you
address.Append(" ");
address.Append(ort);
if (!string.IsNullOrEmpty(telefon))
{
address.Append(newLine);
address.Append("Telefon:: ");
address.Append(telefon);
}
if (!string.IsNullOfEmpty(natel))
{
address.Append(newLine);
address.Append("Natel: ");
address.Append(natel);
}
if (!string.IsNullOrEmpty(mail))
{
address.Append(newLine);
address.Append("E-Mail: ");
address.Append(mail);
}
return address.ToString();
Ok i got it now. I found out that appointments are stored in html format.
So i tried to use the html entity for \r\n, .That didn't work. I finally solved the problem by using the br tag
While you're absolutely correct about using <br/>, newline is not the only thing Exchange eats in notes/appointment body.
I ended up with the following code:
Regex NewlineRegex = new Regex("(\r\n)|(\r)|(\n)");
string valueToWrite = NewlineRegex.Replace(
SecurityElement.Escape(fieldValue), "<br/>")
.Replace(" ", " ")
.Replace("'", "'"); // ' is not in HTML.
And even after that you will read back an extra "\r\n" in the end of the body/notes, so I have to .TrimEnd() them after reading.
you should try "\r\n"
See http://www.infinitec.de/post/2009/08/25/Exchange-WebServices-Bug-with-Lineendings.aspx
I don't know what is wrong with the following string:
"Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")"
I can't get the concatenated string. I am getting Report(29-Dec-2009. That's all and
the rest gets left out from the string.
What is the reason?
Try this:
string filename =
String.Format(
"Report({0:dd-MMM-yyyy} to {1:dd-MMM-yyyy})",
System.DateTime.Now, System.DateTime.Now.AddMonths(-1));
EDIT: Since in your download box you got your filename broken in first whitespace, you could to try ONE of these:
filename = HttpUtility.UrlEncode(filename); // OR
filename = """" + filename + """";
Seems some browsers doesn't handles whitespaces very nicely: Filenames with spaces are truncated upon download. Please check it you can to download other filenames with whitespace in other sites.
You need to assign it to something:
string s = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " + System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")"
Update: I just saw your update to the question. How are you displaying the string? I'm guessing that you are displaying it in a GUI and the label is too short to display the complete text.
Try this:
string newstring =
string.Format(
"Report ({0} to {1})",
System.DateTime.Now.ToString("dd-MMM-yyyy"),
System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
);
What are you assigning the result to? It would be easier to read the code if you used string.Format
You are not assigning the concatenated result to anything, so can't use it:
string myConcatenated = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + ")";
Using this code...
string test = "Report(" + System.DateTime.Now.ToString("dd-MMM-yyyy") + " to " +
System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy") + ")";
I saw the following result.
Report(29-Dec-2009 to 29-Nov-2009)
It could be that the string is being truncated later on. Make sure that you set a breakpoint right after this code is run and check the value of the variable to which it is assigned (test in my case).
If, as in your previous question, you are using this value to create a file, it may be that it's the space before "to" that is causing the problem. Try to use:
"Report("
+ System.DateTime.Now.ToString("dd-MMM-yyyy")
+ "To"
+ System.DateTime.Now.AddMonths(-1).ToString("dd-MMM-yyyy")
+ ")"
instead and see if that fixes it.
If that does fix it, you'll probably need to either figure out how to quote the entire file name so it's not treated as the three separate arguments, "Report(29-Dec-2009", "to" and "29-Nov-2009)". Or simply leave your reports names without spaces.
I'd choose the latter but then I'm fundamentally opposed to spaces in filenames - they make simple scripts so much harder to write :-)