How to Replace two slashes to one single slashe? C# - c#

I have a problem with somwthing about replace char. I tryied a lot of links but get the same problem to replace (\\) to (\)
here is my code:
string mystringA = textBox.text
string mystringB = mystringA.Replace("\\", "\"");
The result of mystringB stay the same as mystringA.
I am saying because I put a debug mode to see the result
My textBox.txt = C:\Users\Braulio Jose\Desktop\impressora\myfoto.png
I have to replace the double quotes because I want to delete this photo in another place but when I follow the path, mystringA put another quote, and I this path don't exist
I am using visual studio 2013 and C# Language.
Some help. thank you

Due to the fact, that your question is about quotes, but your code is about slashes, it's hard to guess what your real problem is.
But here is some sample code for both replacements:
var replaceQuotes = "Some text with \"\"double quotes\"\"";
var replacedQuotes = replaceQuotes.Replace("\"\"", "\"");
Console.WriteLine("Before: " + replaceQuotes);
Console.WriteLine("After: " + replacedQuotes);
Console.WriteLine();
var replaceSlashes = "Some text with \\\\double slashes\\\\";
var replacedSlashes = replaceSlashes.Replace("\\\\", "\\");
Console.WriteLine("Before: " + replaceSlashes);
Console.WriteLine("After: " + replacedSlashes);
And here the output:
Before: Some text with ""double quotes""
After: Some text with "double quotes"
Before: Some text with \\double slashes\\
After: Some text with \double slashes\

Related

Add double quotes to a list to display in a label

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 ".

String formatting for rich text box with constant and dynamic text

Currently I am attempting to create a dictionary which maps a selected item form a list view with a corresponding string output in a rich text box. I would like to bold specific text in the string, which will always be the same (constant) and also adding dynamic text to the string that would change.
Something like this:
ID: 8494903282
Where ID is the constant text I need bolded and the numbers would be a dynamic ID that changes. I will need to have multiple lines with different data in this format which will be changing:
ID: 8494903282
Name: Some Name
Date: 3/15/2018
Currently I have a rich text box to output to and I am trying to use some string formatting to do what I want but this is not working correctly. Essentially I need a string value I can store in a dictionary so when an item gets selected I can just set the rtf property of the text box to the value of that dictionary item.
Below I have my current format string I am attempting to set the rtf property to:
string s1 = string.Format(#"{{\rtf1\ansi \b Commit ID: \b0 {0}\line}}", entry.ID);
string s2 = string.Format(#"{{\b Author: \b0 {0}\line}}", entry.Author);
string s3 = string.Format(#"{{\b Date: \b0 {0}\line}}", entry.Date.ToString("d"));
string s4 = Environment.NewLine + Environment.NewLine + entry.Message;
contents = (s1 + s2 + s3 + s4);
Then setting the rtf property of my rich text box:
LogContentsTB.Rtf = Logs[LogNamesLV.SelectedItems[0].Name];
Where logs is a dictionary of the form < string, string > that holds the format string for the specific item.
However, I get the following output rather than my expected output:
This is the correct form of output for the first item but nothing else appears. If there are any other ways to do this I am open to suggestion.
After doing some light reading on the rtf syntax I noticed that I was trying to close off each string with curly braces. Curly braces are used for RTF groups. For some reason the rich text box in windows forms did not play well with that.
Another thing to notice is that the string.format method was probably the main culprit for cause of issues with this type of formatting. In my answer I do not use it but rather just add the string directly into the rtf formatted string i.e. < format >< variable string >< ending format >
If you look at NetMage's response, you will notice he only puts an opening brace on the very first string, s1. This is to group the whole string. But we need to add a closing brace on the final string, s4, to finish the grouping. Below is the final code and screenshot that worked for my application.
string s1 = #"{\rtf1\ansi\b ID: \b0 " + entry.ID + #" \line\line";
string s2 = #"\b Author: \b0 " + entry.Author + #" \line\line";
string s3 = #"\b Date: \b0 " + entry.Date.ToString("d") + #" \line\line ";
string s4 = entry.Message + #"}";
contents = s1 + s2 + s3 + s4;
Thanks for pointing me in the right direction!
I think your RTF formatting is wrong. You could try:
string s1 = string.Format(#"{{\rtf1\ansi\r\b Commit ID:\b0 {0}\line\r", entry.ID);
string s2 = string.Format(#"\b Author: \b0 {0}\line\r", entry.Author);
string s3 = string.Format(#"\b Date: \b0 {0}\line\r", entry.Date.ToString("d"));
string s4 = Environment.NewLine + Environment.NewLine + entry.Message + "}}";
contents = (s1 + s2 + s3 + s4);

Display new line of text document after pressing enter key

I'm currently pooping out, I am having trouble displaying lines from a text document. What I mean is whenever I press enter it displays a new line of the text document.
Thanks
This is I guess some code, this is referenced to a text document and received a definition, from here it receives it and gets it ready to display but I just need it to read from say result one line at a time every time I press enter
First line wont work this is the first line ->>>>
var result = GetLinesWithWord(i1, #"" + Path + "/dict2.txt");
// Display the results.
foreach (var line1 in result)
{
//word maxlength
const int MaxLength = 82;
var name1 = line1;
if (name1.Length > MaxLength)
name1 = name1.Substring(0, MaxLength);
Console.WriteLine(name1 + "\r");
string boc1 = name1;
string foc1 = i1;
System.IO.File.AppendAllText (#"" + Path + "/" + n + ".txt", foc1 + "\n" + boc1 + "\n");
If I understand you correctly (especcialy your comment to #MairajAhmad) you want to dump the text file line by line, waiting for the user to press a key after each line.
Look at the post here. I think, that is what you need. It's basically a call to Console.ReadKey(true). The mentioned post listen's for the escape key, but it should still illustrate what to do.

Reading from a file in C# without the newline character

i want to read from a text file in C#. But I want all the lines in the file to be concatenated into one line.
for example if i have in the file as
ABCD
EFGH
I need to read ABCDEFGH as one line.
I can do this by reading one line at a time from the file and concatenating that line to a string in a loop. But are there any faster method to do this?
string.Join(" ", File.ReadAllLines("path"));
Replace " " with "" or any other alternative "line-separator"
Example file:
some line
some other line
and yet another one
With " " as separator:
some line some other line and yet another one
With "" as separator:
some linesome other lineand yet another one
Use this:
using (System.IO.StreamReader myFile = new System.IO.StreamReader("test.txt")) {
string myString = myFile.ReadToEnd().Replace(Environment.NewLine, "");
}
What is a one line for you?
If you want to put the entire content of a file into a string, you could do
string fileContent = File.ReadAllText(#"c:\sometext.txt");
If you want your string without newline characters you could do
fileContent = fileContent.Replace(Environment.NewLine, " ");
string file = File.ReadAllText("text.txt").Replace("\r\n", " ");

String concatenation doesn't seem to work in C#

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

Categories