Getting a complex text as a string - c#

abc #cnn("sujsl d(*) sfv #nor dsf ",dn,".",#tn); ator '`,`') sds
#cns1 or '`,"\','\",`') fdhg #cns2 sf \",dn,"; nj
How can I get this text as a string in C#. It can not be simply done due to containing " I would prefer to paste it, someway, as it is, rather than replcaing " with \" as my text already contains things like that
One way is, to paste it in a static text Box (textBox1) and then get it as Text of textbox (If I have to use this in a winform) e.g. string st = textBox1.Text
How this type of text can be got in a string variable without using a textBox?

This is a good case for resource files. They will allow you to enter string literals without having to worry about escaping of any kind and are available to you programmatically. See for reference http://msdn.microsoft.com/en-us/library/3xhwfctz(v=vs.100).aspx

Check out string literals, this may be what you are looking for.

Related

Is there any way to automatically add many properties on VS2019?

Don't judge me, but I have an object with over 100 properties, most of them string.
Is there anyway to automatically add them to the code, without extensive typing?
I have them all in a text file with correct casing. I looked for plug ins, but couldn't find any (maybe using wrong keywords?)
I am assuming your file looks like:
Property
SomeOtherProperty
Test
The easiest way would be to use a CSV -> C# Model generator
Steps
Change it to be comma separated, you can do this with C#:
var path = #"C:\Path\To\Your\File.txt";
var text = File.ReadAllText(path);
text = text.Replace(Environment.NewLine, ",");
File.WriteAllText(path, text);
Open the file and copy its contents to your clipboard.
Now open the C# Class from CSV tool.
Paste the contents and voila you have a C# model!
Notepad++ can do it quite easily.
Open your text file with notepad++ and press Ctrl+H
Fill in the fields like below:
search for (.*)
replace with public string \1 {get;set;}
tick "regular expression"
press "Replace all"
And voilĂ :
Note that this should work with any editor that handle regex (as stated by #Klamsi in the comments section)

how to make string returned by ResourceManager.GetString not verbatim

Okay, I have a string
string textToShow = "this\nrocks"
which when put in label in winforms window will then show
this
rocks
Which is the result I'd like to get. Now, instead of setting the textToShow in the code, I set it in the resource file. When I tried to get the value from resource file using
Properties.Resources.ResourceManager.GetString("textToShow");
the whole string instead will be treated as verbatim, showing
this\nrocks
when put in a label in a winforms window. This is not the result i'm looking for. What's the best way to store strings with special characters in resource file then? I can do string replace for every special characters, like
string.Replace(#"\n", "\n");
but then I need to replace every special characters whenever I call method ResourceManager.GetString, which I think is not the most elegant solution. If there is some ways to make string returned from method ResourceManager.GetString not verbatim, please do tell me.
Thanks
This was already answered here: StackOverflow: How to deal with newline
Basically you have two useful options:
Use shift + enter in the resource manager text editer to add a new line.
Or use String.Format() to replace {0} with \n on read.
The .Net 4.5 framework has the unescape functionality as shown here:
using System.Text.RegularExpressions;
Regex.Unescape(Properties.Resources.ResourceManager.GetString("textToShow"));
solves your issue. Now you can use \n and \u in the resource files.
On the resource editor type "this<shift+enter>rocks" as the resource value.

HttpRequestValidationException workaround

I have a textbox that when user inputs a string such as "<daily" (to signify less than daily) it throws a HttpRequestValidationException. However if there is a space between the less than symbol and the string, it works fine such as "< daily".
I have had it change the value that is submitted in the code behind by using the replace function. For example:
string s = "This is a <test";
if(s.Contains("<")){
s = s.Replace("<", "< "); //I have also used "<" & "<"
}
However, I still get the exception because in the textbox it is still showing it as "<daily". I am wondering if there is a way that if the focus is off the textbox to dynamically add a space to the string?
I understand that the HttpRequestValidationException is not supposed to allow those characters, but it seems to allow if there are spaces. Any thoughts?
It would be nice to know how you use the string in the HttpRequest. Depending on how and where you use we could come up with some ideas.

Can't write hyperlink as a string

I want to write something like this
string input = "<form action=\"http://blabla.com\" method=\"post\">...</form>";
but the backslash() where .com ends merge with link... what i can do?
So link becoming http://blabla.com\
This might be more readable with strings that contain both slashes and double quotes. It is for me.
string input = #"<form action=""http://blabla.com"" method=""post"">...</form>";
Also note that a single quote is acceptable in HTML, so this should work too:
string input = #"<form action='http://blabla.com' method='post'>...</form>";
Here's some additional info on literals in C#
http://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx
This is only visually inside the Visual Studio Editor. When running, your program should work fine.

multiline textbox to string

I have a multiline textbox that I wish to convert to a string,
I found this
string textBoxValue = textBox1.Text.Replace(Environment.NewLine,"TOKEN");
But dont understand TOKEN what is TOKEN? whitespace or /n newline ?
If this is the incorrect answer then Please let me know of the correct way of doing this
Thanks
In the code snippet you gave, "TOKEN" is any value you wish to insert, such as an HTML <br /> tag, more Environment.NewLines for formatting, or just some random delimiter that will later allow you to split the text on it.
A very simple example:
string text = textBox1.Text.Replace(Environment.NewLine, "^"); // a random token
string[] lines = test.Split( '^' );
If you are handling input from a textbox available on the web, you also need to take into account XSS (http://en.wikipedia.org/wiki/Cross-site_scripting). Also, in a real scenario I would split on a more complex token and make sure to handle multiple carriage returns in the input value.
EDIT: now that I see your actual requirements, this code may do what you need:
// replace newlines with a single whitespace
string text = textBox1.Text.Replace(Environment.NewLine, " ");
EDIT #2:
further I need to enter this data into
SQLite and rewrite his whole
application, The company does not wish
to have information from the previos
application inputted to the new
database, there are hyperlinks etc
inbedded in the content , so if there
is a way I can make the text box only
accept RAW data this would be the
best.
Regular Expressions are the way to go for something like this, unless the data is structured enough to load into an XML or HTML DOM and process. You can build regular expressions in a variety of tools (do a Google search for a free online tester and you will find many). Once you have determined the expressions you need, you can use the Regex object in C# to match, replace, etc.
http://msdn.microsoft.com/en-us/library/ms228595(VS.80).aspx
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace(v=VS.100).aspx
As it stands, "TOKEN" is just a meangingless string, unless it is elsewhere in your code? You can replace "TOKEN" with any text you like.
Edit:
Okay, so you say you're removing NewLine's from your client's text. So you would do it like this. Paste their text into a textBox called textBox2, then use the following:
textBox2.Text = textBox2.Text.Replace(Environment.NewLine, string.Empty);

Categories

Resources