Before displaying my view in method of controller I replace character ; for newline symbol.
reservationHistory.ReservedHouses=reservationHistory.ReservedHouses.Replace(";", "\n");
In my view I display this field in that way.
#Html.DisplayFor(model => model.ReservedHouses)
However is not a good solution. Should I change symbol o new line in method Replace or use another method to display field instead of Html.DisplayFor?
reservationHistory.ReservedHouses=reservationHistory.ReservedHouses.Replace(";", "\n");
Is fine althought you could swap "\n" for Environment.NewLine
Then in your view try doing the following:
#MvcHtmlString.Create(reservationHistory.ReservedHouses.Replace(Environment.NewLine, "<br />"))
Related
Example: If the display name contains "Lambda (?)" then replace with "Lambda (λ").
I have four various substrings that I'm trying to replace
Here's the part of my stringbuilder code:
sb.Append("<div><a href=\"" + item.URLAliasName + "\" >" + (string.IsNullOrEmpty(item.DisplayName) ? " " : item.DisplayName) + "</a>"...
How do I replace "Lambda (?)" if contained inside item.DisplayName? It may be found anywhere in the display name.
Perhaps something like this would work?
item.DisplayName.Replace("Lambda (?)", "Lambda (λ)");
You can use String.Replace it takes in two strings as parameters, the value to replace and what to replace it with. It returns the resulting string.
For example: String replacedString = String.Replace("Lambda(?)", "Lambda(λ");
I have the following Regex pattern to remove all characters after the 2 line breaks.
(?<=.+[\r\n]+.+[\r\n]+)([\s\S]*)
My problem here is that I also wanted to add a check for a specific text, for example after that 2 line breaks and if it is found, do not include it.
And here is how I do it on my c# code:
string newComment = string.IsNullOrEmpty(regexPattern) ? emailBody : new Regex(regexPattern, RegexOptions.IgnoreCase).Replace(emailBody, string.Empty);
EDIT
I wanted to look for a specific text, for example "This is a signature:" then if it is found, it should not be included and anything after it also, while maintaining the current design which everything after 2 line breaks will not be included
Sample strings:
string body = "Try comment.";
string additionalBody = "This is a signature";
string newBody = body + System.Environment.NewLine + additionalBody + System.Environment.NewLine + "asd Asd";
So the newBody should result to 3 paragraphs text.
It should display the "Try comment" only.
Possible scenarios may be:
1) On the first or second paragraph, the text can be present and should be removed automatically.
2) If the automated signature is not present but there is 3 paragraphs, remove the last paragraph.
Try this:
(?<=(?>.+[\r\n]+){2})(?:(?!\bThis is a signature\b)[\s\S])*
How about simply:
(?<=(?:.+[\r\n]+){2})([\s\S]*)This is a signature
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)
I have a multiline textbox. I am entering recipe ingredients one by one in each line by using enter key. I am saving this to database in a single field, but on display I need to split them to show them line by line. How can I achieve this?
No special effort should be needed.
If the user enters a multi-line string and you save it to the database like that, when you fetch it from the database again, it will still be a multi-line string.
When you display, have such code:
strValueFromDb.Split('\n').ToList().ForEach(line =>
{
lbDisplay.Text += line + "<hr />";
});
This example will show each line followed by <hr /> you can do whatever you want of course.
Can't your database handle '\n'? If not (?), then replace it with a 'placeholder' such as '::' and then swap it back to '\n' when displaying it.
You can use Split() method of a string:
string[] lines = "Line 1\r\nLine2\r\nLine 3".Split(new string[] { Environment.Newline }, StringSplitOptions.None);
You can use Environment.Newline
txtRecipe.Text = "First line" + Environment.Newline + "Second line";