Textbox.Text new Line? [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
again a pretty simple question(i think so) but i can't find something via google. I think i'm using the wrong keywords but i don't know how to google it right.
So what i want to do is:
I have a text box (just a normal textbox not a richtextbox),
the input text is coming from a database.
Example String: "TextA \nTextB"
If i read out the string from the textbox the output is: "TextA \\nTextB".
I'm creating a pdf with the output from the textbox, so actual in the pdf you can read "TextA \nTextB", but there is no new line. If i create a normal string with "TextA \nTextB". I can see the new line in the pdf.
So basicly the question is:
What i have to change, that mit output string is the same as the input string?
What i already tried:
TextA \r\nTextB
TextA'\n'TextB
TextA"\n"TextB
Thank you

(from comments)
actual it contains a slash n
and
it returns a 92
(where "it" is the character code at the index of the ...\n..., so: a slash)
If the contents are a slash and an n, then: that isn't a newline, and the tools are doing the correct thing by representing it as a slash and an n. The \n syntax only applies to C# literals, as a way of telling the compiler to include something that isn't actually a slash and an n.
A C# literal expressed as "foo\nbar" is the string with contents:
foo
bar
(a newline)
However, a string with contents foo\nbar is the string with contents:
foo\nbar
(a slash and an n, no newlines)
For completeness, there's also C# verbatim string literals, expressed as #"foo\nbar" which is also the string with contents
foo\nbar
(a slash and an n, no newlines)
When you load a string value from the database, you aren't using C# literals - you are just dealing with strings. So a slash and an n means: a slash and an n. Not a newline.
So: if possible, store what you actually mean. The text in the database should contain a newline, not a slash and an n. If the line-breaks don't show as line breaks when you do select TheValue from TheTable, it isn't stored correctly.
If that isn't an option, you'll have to use some kind of text replace (maybe .Replace(#"\r\n","\r\n").Replace(#"\n", "\n").Replace(#"\r", "\r")), but you'll need to think about false positives, especially if your context can include things like code examples, i.e. slash followed by n is likely to appear to mean a slash followed by n.
In terms of SQL literals; if you wanted to do this in SQL, SQL literals allow newlines directly, so:
declare #someString nvarchar(200) = N'foo
bar'; -- this contains a newline
(although whether it contains CR, LF, CRLF or LFCR may depend on your editor's line-endings)
However, usually you would be populating values by SQL parameters, i.e.
cmd.CommandText = "... #val ..."; // some SQL
cmd.Parameters.AddWithValue("#val", someStringThatHasNewlines);
cmd.ExecuteNonQuery();
where someStringThatHasNewlines contains newline characters (ASCII 10/13), not slash+n.

Related

How to align big one line string in visual studio

Hello i have maybe stupid question, how can i write for example
this line of code, which may represent big sql statement (dozens of rows)
string sql = "SELECT A,B,C FROM TableX";
to following block code format, which is more transparent, simpler to edit but i need also to have one line string ?
string sql = "SELECT A,
B,
C
FROM
TableX";
But i dont want to have \n characters in the string, i want that block was also ONE LINE string.
Thank you
You could add # in front of the string. This turns it into a verbatim string which is allowed to span multiple lines.
This does introduce line breaks (and possible lots of extra spaces, depending on indention), but you can easily remove those.

How to find a string in a column that contains a lengthy "word" or set of characters

I am looking for an unusually long word or grouping of characters in a specific column of data that contains notes written by users. For example, if something like this -
I am looking for an unusuallylongwordorgroupingofcharactersina specific column
exists, I need to find it so I can add spaces if necessary. My question is: How do I find a word or set of characters that exceeds a certain number of characters?
The problem is that somewhere in this data, an unusually long word or grouping of characters is being parsed and causing an OutOfMemoryException, so I need to find the source and fix it.
You could use a regex in C# if the raw string fits in memory: \w{15,} gives you words at least 15 characters in length. There are many ways to tweak this (lookahead, lookbehind, more specific character classes, etc.).
You can write a C# stored procedure that can be run against the column in question.
It would split the column into an array of strings containing a word Then you can easily find the largest word in the column.
see http://msdn.microsoft.com/en-us/library/vstudio/zxsa8hkf%28v=vs.100%29.aspx
for details on how to, write install and debug a C# stored procedure in SQL Server
Using the answers given, I created a program that pulls the data and tosses each word into a list. It then pulls words of a given length (in my case, I did greater than 20 characters) and found the bad "word". Now I can fix the data.
I appreciate all your help, guys.

Why SQL Server stored procedure doesn't recognize text from Visual Studio?

Ok. Will try to explain with images... This is my SQL Server and my query:
As you see I getting the result. But then I start my app in VS2013, put break point when I want to call my stored procedure and copy text from VS:
And paste Name in Qhuery:
But I didn't get the result! The names ABSOLUTELY THE SAME!
This Query doesnt't work:
SELECT TOP 1 [Employee].[EmployeeID]
FROM [Employee]
WHERE [Employee].[FullName] = 'Brad Oelmann'
I agree the initial suspect is a "special character" that shows up as whitespace pasting in SSMS.
It has happened to me filtering client data with t-sql.
To replace special characters, there is a good starting point here:
.NET replace non-printable ASCII with string representation of hex code
In that case, they're looking for "control characters" in particular and doing a fancy replacement, but the idea of finding the special characters RegEx is the same.
You can look at all kinds of special sets of characters here:
http://msdn.microsoft.com/en-us/library/20bw873z(v=vs.110).aspx
But it might be easier to define what you do want if you are doing something specific like a name.
For example, you can replace anything that isn't an English letter (for one example) with a space:
str = System.Text.RegularExpressions.Regex.Replace( _
str, _
"[^a-zA-Z]", _
" ")
It's really stupid, but I got simple solution. Since my DB Table contains only ~50 records, I retyped all names and now it works. So the problem was not in VS but in SQL Server side.
If somebady will have similar problem, first of all try to update data in your table somehow. You can try to select all data, copy-paste in in notepad and put it back in SQL Server.

Regular Expression to match a quoted string embedded in another quoted string

I have a data source that is comma-delimited, and quote-qualified. A CSV. However, the data source provider sometimes does some wonky things. I've compensated for all but one of them (we read in the file line-by-line, then write it back out after cleansing), and I'm looking to solve the last remaining problem when my regex-fu is pretty weak.
Matching a Quoted String inside of another Quoted String
So here is our example string...
"foobar", 356, "Lieu-dit "chez Métral", Chilly, FR", "-1,000.09", 467, "barfoo", 1,345,456,235,231, "935.18"
I am looking to match the substring "chez Métral", in order to replace it with the substring chez Métral. Ideally, in as few lines of code as possible. The final goal is to write the line back out (or return it as a method return value) with the replacement already done.
So our example string would end up as...
"foobar", 356, "Lieu-dit chez Métral, Chilly, FR", "-1,000.09", 467, "barfoo", 1,345,456,235,231, "935.18"
I know I could define a pattern such as (?<quotedstring>\"\w+[^,]+\") to match quoted strings, but my regex-fu is weak (database developer, almost never use C#), so I'm not sure how to match another quoted string within the named group quotedstring.
FYI: For those noticing the large integer that is formatted with commas but not quote-qualified, that's already handled. As is the random use of row-delimiters (sometimes CR, sometimes LF). As other problems...
Replace with this regex
(?<!,\s*|^)"([^",]*)"
now replace it with $1
try it here
escaping " with "" it would become
(?<!,\s*|^)""([^"",]*)""

.NET string IndexOf unexpected result

A string variable str contains the following somewhere inside it: se\">
I'm trying to find the beginning of it using:
str.IndexOf("se\\\">")
which returns -1
Why isn't it finding the substring?
Note: due to editing the snippet showed 5x \ for a while, the original had 3 in a row.
Your code is in fact searching for 'se\\">'. When searching for strings including backslashes I usually find it easier to use verbatim strings:
str.IndexOf(#"se\"">")
In this case you also have a quote in the search string, so there is still some escaping, but I personally find it easier to read.
Update: my answer was based on the edit that introduced extra slashes in the parameter to the IndexOf call. Based on current version, I would place my bet on str simply not containing the expected character sequence.
Update 2:
Based on the comments on this answer, it seems to be some confusion regarding the role of the '\' character in the strings. When you inspect a string in the Visual Studio debugger, it will be displayed with escaping characters.
So, if you have a text box and type 'c:\' in it, inspecting the Text property in the debugger will show 'c:\\'. An extra backslash is added for escaping purposes. The actual string content is still 'c:\' (which can be verified by checking the Length property of the string; it will be 3, not 4).
If we take the following string (taken from the comment below)
" '<em
class=\"correct_response\">a
night light</em><br
/><br /><table
width=\"100%\"><tr><td
class=\"right\">Ingrid</td></tr></table>')"
...the \" sequences are simply escaped quotation marks; the backslashes are not part of the string content. So, you are in fact looking for 'se">', not 'se\">'. Either of these will work:
str.IndexOf(#"se"">"); // verbatim string; escape quotation mark by doubling it
str.IndexOf("se\">"); // regular string; escape quotation mark using backslash
This works:
string str = "<case\\\">";
int i = str.IndexOf("se\\\">"); // i = 3
Maybe you're not correctly escaping one of the two strings?
EDIT there's an extra couple of \ in the string you are searching for.
Maybe the str variable does not actually contain the backslash.
It may be just that when you mouse over the variable while debugging, the debugger tooltip will show the escape character.
e.g. If you put a breakpoint after this assignment
string str = "123\"456";
the tooltip will show 123\"456 and not 123"456.
However if you click on the visualize icon, you will get the correct string 123"456
Following code:
public static void RunSnippet()
{
string s = File.ReadAllText (#"D:\txt.txt");
Console.WriteLine (s);
int i = s.IndexOf("se\\\">");
Console.WriteLine (i);
}
Gives following output:
some text before se\"> some text after
17
Seems like working to me...
TextBox2.Text = TextBox1.Text.IndexOf("se\"">")
seems to work in VB.
DoubleQuotes within a string need to be specified like "" Also consider using verbatim strings - So an example would be
var source = #"abdefghise\"">jklmon";
Console.WriteLine(source.IndexOf(#"se\"">")); // returns 8
If you are looking for se\">
then
str.IndexOf(#"se\"">")
is less error-prone. Note the double "" and single \
Edit, after the comment: it seems like the string may contain ecaping itself, in which case in se\"> the \" was an escaped quote, so the literal text is simply se"> and the string to use is Indexof("se\">")

Categories

Resources