The Output of this code
string fileDateTime = "StepsGA-" + DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss") + ".txt";
string fname = System.IO.Path.Combine(System.Environment.CurrentDirectory, fileDateTime);
File.WriteAllText(fname, txtSteps.Text);
doesn't preserve newline as it should be. All the contents of the textbox are printed in a horizontal long line ! Where have I gone wrong? Thanks !
Got the answer ! Instead of \n I needed to use Environment.NewLine.
Related
In my code i got a very long and complicated string that I want to save as a literal string
At the moment my line is 1200 character long.
And I want to separate the lines in the way which every line it wouldn't be longer than 200 characters
string comlexLine = #"{""A"": {""B"": [{""C"": {""D"": {""E"":""+"" ""/F;
I would like to separate it into shorter lines so the code would be more readable.
At the moment when I enter a new line, because it is a literal string a \r is entering to the string
for example:
string comlexLine = #"{""A"": {""B"": "
+ "[{""C"": {""D"": {""E"":""+"" ""/F;
Console.WriteLine(comlexLine);
would print:
#"{""A"": {""B"": //r "[{""C"": {""D"": {""E"":""+"" ""/F
I prefer not to split it to different constant and also to use a literal string.
Is there any solution?
Use Environment.NewLine instead for including a new line in your string. i would rather make it like below using a back slash \ to escape the extra double quotes
string comlexLine = "{\"A\": {\"B\": " + Environment.NewLine + "[{\"C\": {\"D\": {\"E\":\"+\" \"/F";
Console.WriteLine(comlexLine);
Try not using the literal and escaping the double quotes with a slash.
string comlexLine = "{\"A\": {\"B\": [{\"C\": "
+ "{\"D\": {\"E\":\"+\" \"/F";
If I use that, it doesn't introduce the //r.
I Have a string "User " + nick + " has been connected" (WinForms)
Can you please tell me, how to get nick from that string ? (it will be printed as 1 string)
It's behind "User" and "has" words.
Thanks
You can use regex with ^User (.*?) has been connected$
var myUser = Regex.Match(inputString, "^User (.*?) has been connected$").Groups[1].Value;
This could do:
string nickname = yourString.Replace("User ", "").Replace(" has been connected", "");
The boilerplate text is constant so read past the 5 characters of "user " upto the length of the string less the length of the characters you always know will be there.
string name = text.Substring(5, text.Length - 24);
I am new to c sharp
can anybody say what the mistake
string cPict= "Picture\"+firstSelectedItem+".jpg";
where
"Picture\" = folder
firstSelectedItem = Employee Number
".jpg" = file extension
getting following error
string does not contain definition for jpg
please help
thanks in advance
The problem is that in "\"+firstSelectedItem all is treated as string, even the firstSelectedItem variable because you've used the \-character to escape the following ".
You either have to
escape the \-character by another one,
use a verbatim string literal or
use the Path-class, especially Path.Combine:
1)
string cPict = "Picture\\" + firstSelectedItem + ".jpg";
2)
string cPict = #"Picture\" + firstSelectedItem + ".jpg";
3)
string cPict = Path.Combine("Picture", firstSelectedItem + ".jpg");
You can replace it with normal slash like that:
string cPict= "Picture/"+firstSelectedItem+".jpg";
The \ is a special character that escapes the next character in a string, therefore, according to the compiler then + firstSelectedItem + is still part of the string. Your code should look like one of the following:
string cPict = #"Picture\" +
or:
string cPict = "Picture\\" +
and that should work.
you need to escape the backslash \ character
string cPict= "Picture\\"+firstSelectedItem+".jpg";
learn about Escape Sequences here
The solution is to add double slashes as below:
string cPict= "Picture\\"+firstSelectedItem+".jpg";
"Picture\\"=folder
Doing this
String t = "asd\nasd";
TextBox objTxt = (TextBox)messageBox;
bjTxt.Text = t;
doesnt show
asd
asd
as expected, it shows
asdasd
why, its driving me crazy. TextBox is set to multiline and I can write lines in it manually. Thanks!
TextBox unlike Label and MessageBox ignores "\n" so if you want to get to the newline you will need to use "\r\n" combination. Nevertheless there is a better solution, just use Environment.NewLine and you won't need to think about \r\n combination for a newline.
Either:
String t = "asd\r\nasd";
Or:
String t = "asd" + Environment.NewLine + "asd";
The beautiful thing about Environment.NewLine is that there is no need to worry about the newline in any environment for which you are developing (or at least it should be that way).
EDIT:
I saw your comment, so I'll add few words. You could still use ReadToEnd() and if the text contains only "\n" for newline, you could do the following:
t = t.Replace("\n", "\r\n");
Or:
t = t.Replace("\n", Environment.NewLine);
since Environment.NewLine is essentially a string
Try to use Lines property of TextBox to assign all lines from file:
textBox.Lines = File.ReadAllLines(fileName);
And, as I stated in comment above, for your sample you should use Environment.NewLine for new line to appear in TextBox:
textBox.Text = "asd" + Environment.NewLine + "asd";
Till now I was thinking HttpUtility.HtmlDecode(" ") was a space. But the below code always returns false.
string text = " ";
text = HttpUtility.HtmlDecode(text);
string space = " ";
if (String.Compare(space, text) == 0)
return true;
else
return false;
Same when I try with Server.HtmlDecode()
Why is it so?
Any help would be much appreciated
Thanks,
N
The HTML entity doesn't represent a space, it represents a non-breaking space.
The non-breaking space has character code 160:
string nbspace = "\u00A0";
Also, as Marc Gravell noticed, you have double encoded the code, so you would need to decode it twice to get the character:
string text = " ";
text = HttpUtility.HtmlDecode(HttpUtility.HtmlDecode(text));
I'm cleaning the html like this:
var text = WebUtility.HtmlDecode(html)
.Replace("\u00A0", " ") // Replace non breaking space with space.
.Replace(" ", " ") // Shrink multiple spaces into one space.
.Trim();
The HTML of doesn't mean any kind of space. It means, literally, the text - for example, if you were writing HTML that was talking about HTML, you may need to include the text , which you would do by writing the HTML .
If you had:
string text = " ";
then that would decode to a non-breaking space.
Hello I faced the same issue some minutes ago.
I solved it in this way:
string text = " ";
text = Server.HtmlDecode(text).Trim;
so now:
text = "" is true (the Trim at the end eliminates the space)