printing string in c# using \r\n to start new line - c#

I have a asp.net and c# project.
I need to print this string out, i get it like this from the server:
Working...: 0/0\r\nNavigating: 0/0
It should be printed out in 2 separate lines (using the \r\n), shouldn't it be doing that automatically?
Am i doing something wrong?
I cant change the string but the way i use it, is:
I have a EO progress bar that i send a message like this:
_progressBar.UpdateProgress(_count, progress.Message);
And it displays the message under the progressBar.
The message is the string i posted on top.
Thanks

It seems like your string might contain #"\r\n" instead of "\r\n", case in which you'd have to parse the escape characters yourself.
For example, to replace backslash-r by CR and backslash-n by LF (not the most efficient way to do it):
string s0 = #"A\r\nB"; // s0 would print as: A\r\nB
string s = s0.Replace(#"\r","\r").Replace(#"\n","\n");
Now s contains:
A
B

use <br/> - that is a line break in HTML...
EDIT - as per comment:
string X = #"Working...: 0/0\r\nNavigating: 0/0";
string Y = X.Replace ( #"\r\n", #"<br/>" ); // Result is Working...: 0/0<br/>Navigating: 0/0

The server has to send to the client data that the latter understands. JSON, HTML, XML or whatever data type you're working with.
From the client's point of view \r\n is just a string like any other. In this case you may want to send something like:
Working...: 0/0<br/>Navigating: 0/0
And print it has HTML.

You probably are sending a backslash followed by an n or r and not a \r or \n.
If so you have to parse them yourself or get the output to be \r and \n

Yes it is automatic.
using System;
public class Test
{
public static void Main(string[] args)
{
string mesg = "Working...: 0/0\r\nNavigating: 0/0";
Console.WriteLine(mesg);
}
}
C:\temp>csc Test.cs
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.
Copyright (C) Microsoft Corporation. All rights reserved
C:\temp>Test
Working...: 0/0
Navigating: 0/0
C:\temp

Related

Python replace special group of string using regex

Currently my project runs on .net C# with 4.5.0 framework version. Which also integrated to use IronPython for some python script execution.
Now I am looking for replacing some special text from string with other e.g.
PV1|1|??|??^^^??^^^??|||||??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??|??^??^??^??^^??^^^??
??~??^??^??^??^^??^^^?~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??
??^^??^^^??~??^??^??^??^^??^^^??^??^^??^^^??^??^^??^^^??^^^??^^^??^??^^^??^^^???^??^^^??^??^^^??^^^??^^??^^^??^??^^??^^^??^^^??^^??^^^??|??||||||??^^^.^^^|??||??|||||||||||||||||||??||??|||??||||||
ORC|??||??||||||??|^??^^^??^^^^??
OBR|1|??^??|??^??|??^^^* ??|||??|??||||||||||||||??||??|??|||||||??|
ZDS|??|^TEST^ONLY (TESTONLY)^^^TEST SUPPORT^^^^TEST|??|COMPLETED
OBX|1|??|??^??
from above text I want to replace line for ZDS|??, with OBX|1|?? with rest of the string available in ZDS to be added to OBX. Also want to keep if any OBX string is available in the text as original OBX.
I tried below in Iron Python
import re
regEx = re.compile('^ZDS\|([^|]+)\|([^|]+)\|([^|]+)\|([^|\n\r ]+)')
Message = regEx.sub('OBX|1|FT|||\1~\2~\3~\4|||', Message)
Also
Message = re.sub(r'^ZDS\|([^|]+)\|([^|]+)\|([^|]+)\|([^|\n\r ]+)', 'OBX|1|FT|||\1~\2~\3~\4|||', Message, count=1)
and
Message= str.replace(Message, '^ZDS\|([^|]+)\|([^|]+)\|([^|]+)\|([^|\n\r ]+)','OBX|1|FT|||\1~\2~\3~\4|||')
But all above options did not worked.
I wanted output like
PV1|1|??|??^^^??^^^??|||||??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??|??^??^??^??^^??^^^??
??~??^??^??^??^^??^^^?~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??
??^^??^^^??~??^??^??^??^^??^^^??^??^^??^^^??^??^^??^^^??^^^??^^^??^??^^^??^^^???^??^^^??^??^^^??^^^??^^??^^^??^??^^??^^^??^^^??^^??^^^??|??||||||??^^^.^^^|??||??|||||||||||||||||||??||??|||??||||||
ORC|??||??||||||??|^??^^^??^^^^??
OBR|1|??^??|??^??|??^^^* ??|||??|??||||||||||||||??||??|??|||||||??|
OBX|1|FT|||^TEST^ONLY (TESTONLY)^^^TEST SUPPORT^^^^TEST~??~COMPLETED
OBX|1|??|??^??
Finally I have got answer to my own question
Original message is
Message =
PV1|1|??|??^^^??^^^??|||||??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??|??^??^??^??^^??^^^?? ??~??^??^??^??^^??^^^?~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^??^^??^^^??~??^??^??^?? ??^^??^^^??~??^??^??^??^^??^^^??^??^^??^^^??^??^^??^^^??^^^??^^^??^??^^^??^^^???^??^^^??^??^^^??^^^??^^??^^^??^??^^??^^^??^^^??^^??^^^??|??||||||??^^^.^^^|??||??|||||||||||||||||||??||??|||??||||||
ORC|??||??||||||??|^??^^^??^^^^??
OBR|1|??^??|??^??|??^^^* ??|||??|??||||||||||||||??||??|??|||||||??|
OBX|1|FT|||^TEST^ONLY (TESTONLY)^^^TEST SUPPORT^^^^TEST~??~COMPLETED
OBX|1|??|??^??
and in Python
import re
Message = re.sub(r'ZDS\|([^|]+)\|([^|]+)\|([^|]+)\|([^|\n\r ]+)', 'OBX|1|FT|||'+r'\1~\2~\3~\4|||', Message, count=1)
Message = re.sub(r'ZDS\|([^|]+)\|([^|]+)\|([^|]+)\|([^|\n\r ]+)', 'OBX|1|FT|||$1~$2~$3~$4|||', Message, count=1)

Calling a Python script from C# - changing the script's filepath causes the program to not work

The following code works perfectly without flaw:
public partial class MainForm : Form
{
string pyInterp = File.ReadAllText(Directory.GetCurrentDirectory() + #"\config\pathToPythonInterpreter.txt");
string pyWeather = #"C:\getWeather.py";
public MainForm()
{
InitializeComponent();
UpdateWeather();
}
public void UpdateWeather()
{
labelWeather.Text = PySharp.ExecutePy(pyInterp, pyWeather);
}
}
However, when I change the path to getWeather.py to not be in an arbitrary random location, like this:
string pyWeather = Directory.GetCurrentDirectory() + #"\scripts\getWeather.py";
Then my program no longer obtains the script's output. The script still works: I launched it using IDLE and it completed its function properly. When I call it using C#, the console opens, yet no output is obtained.
The Python script is the following:
from requests import get
from bs4 import BeautifulSoup as soup
r = get("http://www.ilmateenistus.ee/ilm/prognoosid/4-oopaeva-prognoos/")
parsed = soup(r.content, "html.parser")
container = parsed.find("div",{"class":"point kuusiku"})
print(str(container["data-title"]))
(It webscrapes my local weather)
PySharp.ExecutePy() can be viewed here
By far the strangest bug I've ever encountered. Any ideas?
EDIT 1: It seems that C# is indeed reading something from the script. It just appears that this something is.. nothing. I gave the label a default sample text, and after running the program, the label's text is simply changed to an empty string. Hope this incredible discovery helps somehow.
EDIT 2: The program fails to call the script correctly when its filepath contains spaces. For example:
C:\foo bar\testing\pyWeather.py
does not work!
Try surrounding the path that contains spaces with 2 double quotes.
For e.g.
string pyWeather = #"""C:\Users\[myname]\Documents\Visual Studio 2017\Projects\testing\testing\scripts\getWeather.py""";
Similarly, you can do string pyWeather = Directory.GetCurrentDirectory() + #"\scripts\getWeather.py"; followed by pyWeather = "\"" + pyWeather + "\"";.
I would want you to return the answer instead of printing. Printer is an I/O based solution to display. So it will work super fine with IDLE however it may not return results as you expected. I strongly believe this will solve your problem.
instead of printing please try return. I can give more support after trying this.
return(str(container["data-title"]))

Adding a text file to the start of my code causes a parsing error

I'm using this python script to add a copyright spiel to the start of all my C# scripts
import re
import shutil
import os
copyrightloc = 'C:/DATA/pyscripts/copyright.txt'
rootdir = 'C:/DATA/pyscripts/02_CODE'
dstdir = 'C:/DATA/pyscripts/codecopy'
spielfile = open(copyrightloc, "r")
spiel = spielfile.read()
for subdir, dirs, files in os.walk(rootdir):
for file in files:
if file.endswith(".cs"):
with open(subdir+'/'+file, "r+") as codefile , open(dstdir+'/'+file, 'w') as destfile:
destfile.write(spiel+'\n' + codefile.read())
As you can see I am adding the original string to the copyright string and writing it to a new file.
The files look fine when theyre finished but in every file, at the first line of the original file, I get a parsing error. For example, below shows an exerpt from the new file at the end of the copyright speil and the beginning of the original file...
BLAH BLAH BLAH COPYRIGHT
* OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
* CONTRACT, NEGLIGENCE, TORT OR OTHERWISE, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE OR ITS DERIVATIVES.
*/
using UnityEngine; [!!!ERROR IS SHOWN ON THIS LINE!!!]
using System.Collections;
public class Floop : MonoBehaviour {
public rot glorb;
public GameObject foo;
BLAH BLAH BLAH MY CODE
I'm guessing there is some invisible character there like "end of file" or something but I cannot see anything in notepad++ when I select "show all characters" ... If I go to the start of the line in question and hit delete the error goes away..
How can I make my python script avoid this problem?
The MSDN C# style guide says you should not use blocks of asterisks around comments. Can you try prefixing each line of the copyright with //?
Alternatively, you may be able to use this format (note lack of asterisk at the beginning of each line):
/*
copyright here
*/
Maybe your files contains a 'Byte order mark', which are some special characters at the beginning of the file to indicate the encoding.
Check this with a HEX editor if you see some extra characters before the ones that you expect.
If this is the case, then you should use the 'utf-8-sig' encoding. I'm not a python export, but your code could then look like this
...
spielfile = codecs.open(copyrightloc, "r", encoding="utf-8-sig")
...
with codecs.open(subdir+'/'+file, "r+", encoding="utf-8-sig") as codefile , open(dstdir+'/'+file, 'w', encoding="utf-8-sig") as destfile:
There is chance that in your copyright text there is unicode characters not properly encoded try use the codecs module
import re
import shutil
import os
import codecs
copyrightloc = 'C:/DATA/pyscripts/copyright.txt'
rootdir = 'C:/DATA/pyscripts/02_CODE'
dstdir = 'C:/DATA/pyscripts/codecopy'
spielfile = codecs.open(copyrightloc, "r", encoding="utf8")
spiel = spielfile.read()
for subdir, dirs, files in os.walk(rootdir):
for file in files:
if file.endswith(".cs"):
with codecs.open(subdir+'/'+file, "r+",encoding="utf8") as codefile , open(dstdir+'/'+file, 'w') as destfile:
destfile.write(spiel+'\n' + codefile.read())

C# - MessageBox - Message localization in resources and lines breaks

I would like to show MessageBox (WinForms) with string from Resources with lines breaks.
example without Resources (WORKS):
string someMsg = "Message. Details:\n" + someDetails;
MessageBox.Show(someMsg);
Result:
Message. Details:
here are some details
When I move string "Message. Details:\n" into Resources:
string someMsg = GlobalStrings.MsgBoxJustTest + someDetails;
MessageBox.Show(someMsg);
Result:
Message. Details:\nhere are some details
When I moved string with "\n" to resources then MessageBox.Show() stopped to interpret it as newline.
Edit: I'm thinking about: someMsg.Replace(#'\n',Environment.NewLine);
but it's still quite annoying for so simple thing.
if you add that to resources it doesn't take \n as escape charecter
Just open your resource file in notepad to see this and cahnge in XML file(resx)
or
Type your data in notepad with new line.
Copy that and paste in your resource editor
edit:
or
Type/Paste your data into the resource editor UI, select the \n and replace it with an actual linebreak, with Shift-Enter.
You could do something like this (as long as your not .net 2.0):
public static class StringExt
{
public static String FixNewLines(this String str)
{
return str.Replace(#'\n',Environment.NewLine);
}
}
And then:
string someMsg = GlobalStrings.MsgBoxJustTest + someDetails;
MessageBox.Show(someMsg.FixNewLines());
However, this will affect ALL strings in your application (namespace scope)
It's a dirty fix, but it's a quick fix.
Personally, I would just fix my logic all the way through, rather than do something like the above.
Maybe you can Open the resx file as code and add the line breaks directly in the
XML
OR
Possibly they get lost when reading due to escape character maybe try using \\
One easy solution is to store “placeholders” in a resource strings. For instane, this string is stored in *.resx under “MessageDetails” key: "Message. Details:{0}{1}". Then, in your code, use it like this:
MessageBox.Show(String.Format(GlobalStrings.MessageDetails, Environment.NewLine, #"The message"));
The advantage here is a portability, as you can see.

Does C# have an equivalent to JavaScript's encodeURIComponent()?

In JavaScript:
encodeURIComponent("©√") == "%C2%A9%E2%88%9A"
Is there an equivalent for C# applications? For escaping HTML characters I used:
txtOut.Text = Regex.Replace(txtIn.Text, #"[\u0080-\uFFFF]",
m => #"&#" + ((int)m.Value[0]).ToString() + ";");
But I'm not sure how to convert the match to the correct hexadecimal format that JS uses. For example this code:
txtOut.Text = Regex.Replace(txtIn.Text, #"[\u0080-\uFFFF]",
m => #"%" + String.Format("{0:x}", ((int)m.Value[0])));
Returns "%a9%221a" for "©√" instead of "%C2%A9%E2%88%9A". It looks like I need to split the string up into bytes or something.
Edit: This is for a windows app, the only items available in System.Web are: AspNetHostingPermission, AspNetHostingPermissionAttribute, and AspNetHostingPermissionLevel.
Uri.EscapeDataString or HttpUtility.UrlEncode is the correct way to escape a string meant to be part of a URL.
Take for example the string "Stack Overflow":
HttpUtility.UrlEncode("Stack Overflow") --> "Stack+Overflow"
Uri.EscapeUriString("Stack Overflow") --> "Stack%20Overflow"
Uri.EscapeDataString("Stack + Overflow") --> Also encodes "+" to "%2b" ---->Stack%20%2B%20%20Overflow
Only the last is correct when used as an actual part of the URL (as opposed to the value of one of the query string parameters)
HttpUtility.HtmlEncode / Decode
HttpUtility.UrlEncode / Decode
You can add a reference to the System.Web assembly if it's not available in your project
I tried to do full compatible analog of javascript's encodeURIComponent for c# and after my 4 hour experiments I found this
c# CODE:
string a = "!##$%^&*()_+ some text here али мамедов баку";
a = System.Web.HttpUtility.UrlEncode(a);
a = a.Replace("+", "%20");
the result is:
!%40%23%24%25%5e%26*()_%2b%20some%20text%20here%20%d0%b0%d0%bb%d0%b8%20%d0%bc%d0%b0%d0%bc%d0%b5%d0%b4%d0%be%d0%b2%20%d0%b1%d0%b0%d0%ba%d1%83
After you decode It with Javascript's decodeURLComponent();
you will get this:
!##$%^&*()_+ some text here али мамедов баку
Thank You for attention
System.Uri.EscapeUriString() didn't seem to do anything, but System.Uri.EscapeDataString() worked for me.
Try Server.UrlEncode(), or System.Web.HttpUtility.UrlEncode() for instances when you don't have access to the Server object. You can also use System.Uri.EscapeUriString() to avoid adding a reference to the System.Web assembly.
For a Windows Store App, you won't have HttpUtility. Instead, you have:
For an URI, before the '?':
System.Uri.EscapeUriString("example.com/Stack Overflow++?")
-> "example.com/Stack%20Overflow++?"
For an URI query name or value, after the '?':
System.Uri.EscapeDataString("Stack Overflow++")
-> "Stack%20Overflow%2B%2B"
For a x-www-form-urlencoded query name or value, in a POST content:
System.Net.WebUtility.UrlEncode("Stack Overflow++")
-> "Stack+Overflow%2B%2B"
You can use the Server object in the System.Web namespace
Server.UrlEncode, Server.UrlDecode, Server.HtmlEncode, and Server.HtmlDecode.
Edit: poster added that this was a windows application and not a web one as one would believe. The items listed above would be available from the HttpUtility class inside System.Web which must be added as a reference to the project.

Categories

Resources