I have two asp.net pages. both pages have many form controls including textarea control.
on one page when user enter the data with enter key including the textarea post the enter key as '\r\n' and on other page enter key post as '\n'.
i don't understand why the behavior is different. on both pages.
there are different meaning of both as below
\r = CR (Carriage Return) // Used as a new line character in Mac OS before X
\n = LF (Line Feed) // Used as a new line character in Unix/Mac OS X
\r\n = CR + LF // Used as a new line character in Windows
You can visit this link
Difference between \n and \r?
Edit 1
IE returns "\r\n" to indicate newlines. FF returns "\n" in this case.
References What's with the line break variations in C# and ASP.NET? (\r\n vs.\n)
Related
Last year I asked a question about copy the entry value in xamarin forms.
When I test it now the white spaces in the text are filled by a + symbol. Also when pasting the emojis it is not working properly.
I am using Xamarin.Plugins.Clipboard NuGet package for copy the text to the clipboard. When copy text to clipboard I am using the following code:
CrossClipboard.Current.SetText(message);
When long press in the device it shows the paste option. I am using that option for pasting the copied text.
Please suggest a solution for avoiding the + symbol? Also for emoji copy paste.
Thanks in advance.
Problem should need to WebUtility.UrlDecode(String) the text :
Converts a string that has been encoded for transmission in a URL into a decoded string.
If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. URL encoding converts characters that are not allowed in a URL into equivalent hexadecimal escape sequences. The UrlEncode method creates a URL-encoded string.
URL decoding replaces hexadecimal escape sequences with corresponding ASCII character equivalents. For example, when embedded in a block of URL-encoded text, the escape sequences %3c and %3e are decoded into the characters < and >.
Sample as follow:
using System.Net;
Console.WriteLine("Encode:" + WebUtility.UrlEncode("😂"));
// out ==> %F0%9F%98%82
Console.WriteLine("Decode:" + WebUtility.UrlDecode("%F0%9F%98%82"));
// out ==> 😂
Console.WriteLine("Encode:" + WebUtility.UrlEncode("this is a text message"));
// out ==> this+is+a+text+message
Console.WriteLine("Decode:" + WebUtility.UrlDecode("this+is+a+text+message"));
// out ==> this is a text message
Solution:
Not directly CrossClipboard.Current.SetText(message);
Try with CrossClipboard.Current.SetText( WebUtility.UrlDecode(message));
Let's say I have a text field here named "Description". In the Description field, I entered the following text:
This is line 1.
This is line 2.
This is line 3.
When I input these text to the Description field using IE or Chrome and run the SQR process, the description is displayed correctly including the newlines. But when I enter the same description using Firefox (v35.0.1 btw), the description is being printed in the report like this:
This is line 1. This is line 2. This is line 3.
I am sure that in my SQR there are no procedures that strip off the newlines (because it is working with IE and Chrome). I have also validated backend that the description has newlines.
Using the data entered in Firefox, I also tried running the report in IE and Chrome, but the newlines are still not displaying.
Can you tell me why is this happening? Is there a difference between the newlines used by IE, Chrome, and Firefox?
This is a known issue, google firefox textarea newline.
Firefox represents an newline as linefeed character ascii(10) whereas internet explorer as a combination of carriage return character and linefeed character ascii(13) ascii(10).
To ensure the data is saved in the way it's required for your further processes, you could add component record peoplecode, SavePreChange:
/* Newline is Char13)+Char(10) */
YOUR_REC.YOUR_FLD.Value = Substitute(
Substitute(YOUR_REC.YOUR_FLD.Value,
Char(13) | Char(10),
Char(10)),
Char(10),
Char(13) | Char(10));
i am using this to create a new folder
System.IO.Directory.CreateDirectory(#" + somevariable);
the thing is that when i enter the folder c:\newfolder\newfolder in the textbox and is trying to recieve the value up in the controller it is replaced with double slash( \) c:\\newfolder\\newfolder. how would i prevent \ quotes from coming in the path
Secondly the string.replace is also not working for replacing \ with \\
string strText = OrganMeta.vcr_MetaValue;
string gf = strText.Replace("\\", #"\");
"\\" is equivalent to a string of one character, a backslash.
#"\" is also equivalent to a single character, a backslash.
so your Replace method is replacing one form of a backslash with a different form.
try this:
string gf = strText.Replace( #"\\", #"\" );
OR
string gf = strText.Replace( "\\\\", "\\" );
as far as the folder thing goes, Andy is right, it will show a double-backslash in the IDE when in fact there is only one in the string. is there an error when Directory.CreateDirectory() is called? or is the folder created?
Are you sure it's replaced it with \\? If you hover over the variable it will appear to have \\ where there should be a single \ but if you view it in the text visualizer it will show correctly.
Not sure what you mean by string.replace is not working...?? Can you give an example of the code that's not working?
Slashes don't get doubled between the form submit and your controller action.
It's far more likely that you're viewing the result in the debugger or another context that shows two slashes to allow you to distinguish between escaped characters (\n) and a literal slash ().
Write the string to the debug window to verify this.
System.Diagnostics.Debug.WriteLine("SomeText");
I want to read a text file and load its content to my page. I was trying to read the file with StreamReader and then assign the text to a Label, but the text in the page is just one line. I mean the line in the text file wasn't viewed in the page. What should I do?
Perhaps tackling a symptom rather than the problem itself, you can wrap the text file's contents within a <PRE> tag wherein, unlike most other content in HTML, whitespace is respected.
The textfile uses \n or \r\n for getting new lines (\n is a newline and \r is a carriage return - back in the day of typewritters you had to pull the bar thingy back to the left which is called a carriage return and roll the paper down a line to start on the left side of a newline-). Windows generally uses \r\n (although it depends on the application that created the file) mac's generally use \n.
HTML on the other hand uses the <br/> tag for new lines (if you do a viewsource on your current html output you will see the newlines). So all you need to do is replace \r\n or with . You can do this with:
yourstring = yourstring.Replace("\r\n", "<br/>");
or if you don't know for sure what's used in the file or both \r\n and \n are used you can use
yourstring = yourstring.Replace("\r\n", "<br/>").Replace("\n", "<br/>");
be aware that a string is immutable and thus methods like Replace return a copy of the string that has the replacements made. The original string will stay intact.
Please try this
if (System.IO.File.Exists(Server.MapPath("test.txt")))
{
System.IO.StreamReader StreamReader1 = new
System.IO.StreamReader(Server.MapPath("test.txt"));
lblMyLabel.Text= StreamReader1.ReadToEnd();
StreamReader1.Close();
}
HTML doesn't recognize white-space (line breaks, etc) in your text file. If you want to render the content as HTML, you'll need to convert line-breaks into <br/> tags.
Try something like this:
string path = 'c:\myfile.txt':
lblMyLabel.Text = String.Join('<br/>', File.ReadAllLines(path));
So I have an ASP.Net (vb.net) application. It has a textbox and the user is pasting text from Microsoft Word into it. So things like the long dash (charcode 150) are coming through as input. Other examples would be the smart quotes or accented characters. In my app I'm encoding them in xml and passing that to the database as an xml parameter to a sql stored procedure. It gets inserted in the database just as the user entered it.
The problem is the app that reads this data doesn't like these characters. So I need to translate them into the lower ascii (7bit I think) character set. How do I do that? How do I determine what encoding they are in so I can do something like the following. And would just requesting the ASCII equivalent translate them intelligently or do I have to write some code for that?
Also maybe it might be easier to solve this problem in the web page to begin with. When you copy the selection of characters from Word it puts several formats in the clipboard. The straight text one is the one I want. Is there a way to have the html textbox get that text when the user pastes into it? Do I have to set the encoding of the web page somehow?
System.Text.Encoding.ASCII.GetString(System.Text.Encoding.GetEncoding(1251).GetBytes(text))
Code from the app that encodes the input into xml:
Protected Function RequestStringItem( _
ByVal strName As System.String) As System.String
Dim strValue As System.String
strValue = Me.Request.Item(strName)
If Not (strValue Is Nothing) Then
RequestStringItem = strValue.Trim()
Else
RequestStringItem = ""
End If
End Function
' I get the input from the textboxes into an array like this
m_arrInsertDesc(intIndex) = RequestStringItem("txtInsertDesc" & strValue)
m_arrInsertFolder(intIndex) = RequestInt32Item("cboInsertFolder" & strValue)
' create xml file for inserts
strmInsertList = New System.IO.MemoryStream()
wrtInsertList = New System.Xml.XmlTextWriter(strmInsertList, System.Text.Encoding.Unicode)
' start document and add root element
wrtInsertList.WriteStartDocument()
wrtInsertList.WriteStartElement("Root")
' cycle through inserts
For intIndex = 0 To m_intInsertCount - 1
' if there is an insert description
If m_arrInsertDesc(intIndex).Length > 0 Then
' if the insert description is of the appropriate length
If m_arrInsertDesc(intIndex).Length <= 96 Then
' add element to xml
wrtInsertList.WriteStartElement("Insert")
wrtInsertList.WriteAttributeString("insertdesc", m_arrInsertDesc(intIndex))
wrtInsertList.WriteAttributeString("insertfolder", m_arrInsertFolder(intIndex).ToString())
wrtInsertList.WriteEndElement()
' if insert description is too long
Else
m_strError = "ERROR: INSERT DESCRIPTION TOO LONG"
Exit Function
End If
End If
Next
' close root element and document
wrtInsertList.WriteEndElement()
wrtInsertList.WriteEndDocument()
wrtInsertList.Close()
' when I add the xml as a parameter to the stored procedure I do this
cmdAddRequest.Parameters.Add("#insert_list", OdbcType.NText).Value = System.Text.Encoding.Unicode.GetString(strmInsertList.ToArray())
How big is the range of these input characters? 256? (each char fits into a single byte). If that's true, it wouldn't be hard to implement a 256 value lookup table. I haven't toyed with BASIC in years, but basically you'd DIM an array of 256 bytes and fill in the array with translated values, i.e. the 'a'th byte would get 'a' (since it's OK as is) but the 150'th byte would get a hyphen.
This seems to work for long dash to short dash and smart quotes to regular quotes. As my html pages has the following as the content type. But it converts all the accented characters to questions marks. Which is not what the Text version of the clipboard has. So I'm closer, I just think I have the target encoding wrong.
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
System.Text.Encoding.ASCII.GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(m_arrFolderDesc(intIndex)))
Edit: Found the correct target encoding for my purposes which is 1252.
System.Text.Encoding.GetEncoding(1252).GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(m_arrFolderDesc(intIndex)))
If you convert to a non-unicode character set, you will lose some characters in the process. If the legacy app reading the data doesn't need to do any string transformations, you might want to consider using UTF-7, and converting it back once it gets back into the unicode world - this will preserve all special characters.