|
Escape sequences like \r and \n are for the programmer and are converted to their proper one character representation (0x0d and 0x0a, in this case). If you want the length of the string, then assign the string to an intermediate variable first and get the length from that.
string message = "Sending File....\r\n";
int len = message.length;
textBoxConsole.Text = message;
for (int i = 0; i <= 3; i++)
{
textBoxConsole.AppendText("Line 1 of 3");
}
Hope this helps!
Jon
|
|
|
|
|
Thanks for the reply.
When I run your code the length of message is reported as 18, not 20. If I put another "\r\n" at the end of the string and it is reported as 20 not 24. Ignoring all "\"s in the string.
I need this so I know what position I am in the string so I can remove the previous string "Line 1 of 3" and replace it with the new string "Line 1 of 2". While keeping any text already in the console. I am sending thousands of lines so I can't append them into the console.
I tried to copy the textbox.Text to a string and get the length, same results.
Thanks!
|
|
|
|
|
asquaredtech wrote: When I run your code the length of message is reported as 18, not 20. If I put another "\r\n" at the end of the string and it is reported as 20 not 24. Ignoring all "\"s in the string.
It isn't ignoring the slashes. The slashes indicate the start of an escape sequence. The slashes are interpreted by the compiler as if to say this and the next character represent something other than what is displayed in the source code.
In other words, treat the "n" in "\n" as the new line character rather than as an "n".
"\t" is the representation of the tab character (singluar) becuase it is difficult to represent that in code.
"\r\n" represent the character pair carriage return and newline. Although you put four characters in the source code, there are only two character actually generated in the compiled output.
If you do actually need to represent a slash character in a string in your source code then use "\\".
asquaredtech wrote: I need this so I know what position I am in the string so I can remove the previous string "Line 1 of 3" and replace it with the new string "Line 1 of 2". While keeping any text already in the console. I am sending thousands of lines so I can't append them into the console.
One thing you could do is this. If you are displaying this on the Console , use Console.Write and finish your string with "\r" which is a carriage return. Without the newline that normally comes with it, it will return you to the start of the current line. You then overwrite the text that already exists on that line.
Many console applications use this technique when displaying progress status.
|
|
|
|
|
asquaredtech wrote: "\r\n" has length of 2 instead of 4
That count is correct. There are two characters in the string "\r\n", not four.
asquaredtech wrote: The "\n" is cut off because the length was not correct.
What (int) does the .Length method return when you measure the length of the string in the debugger?
"Sending File....\r\n".Length = 18? You expected it to be 20, right?
asquaredtech wrote: textBoxConsole.Text.Remove(len - 1);
..is this the place where your string is a character short?
I are troll
|
|
|
|
|
"T\r\n" : Len = 3, not 5
"T\r\n\r\n" : Len = 5, not 9
All "\"s are not being counted.
I subtract 1 because the .Length is 1 based, the index into the string is 0 based.
Thanks!
|
|
|
|
|
asquaredtech wrote: I subtract 1 because the .Length is 1 based, the index into the string is 0 based.
That is not correct. The length is zero based, a one based length would not make sense.
The value 4 means four characters, not three.
Also, if the length really would have been one based, you would have to add one to the value instead of subtracting one.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Thank you for repeating what I said.
1 based = 1,2,3,4
0 based = 0,1,2,3
length of 4, 4th character has an index of 3 in the string b/c the first items index is 0.
which is why i subtract 1. if I want to remove characters after the 4th character, I send an index of 3.
|
|
|
|
|
asquaredtech wrote: Thank you for repeating what I said.
No, I didn't. What you say doesn't make sense.
asquaredtech wrote: if I want to remove characters after the 4th character, I send an index of 3.
No, you don't. If you send an index of 3 you will not remove characters after the fourth character, you will remove characters from and including the fourth character.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Hi,
1.
the lengths are correct, each character counts as 1, and of course every escaped character such as "\r" and "\n" is one character.
2.
The code you have shown and the description of the symptoms don't fit together:
String.Remove does not modify an existing string, it returns a new one; strings are immutable, you cannot change them, you can only replace them by another string. Hence your Remove statement does not achieve a single thing.
3.
If your output is line-oriented, i.e. each line is a statement and nothing needs to wrap around, then
I strongly suggest using a ListBox rather than a TextBox. It is much faster, and much easier to program
since it handles individual items without needing any concatenation (and no \r \n)
Remark: if you ever want to copy text from your XxxBox, a ListBox only supports integral-line selections,
and you then would have to do the concatenation programmatically.
|
|
|
|
|
Hi Luc
1. I understand this, I was hoping there was a way to count the "\"s.
2. The remove function allows me to remove the string "Line 1 of 3" from the text box, then append the string "Line 2 of 3" which is returned from method I call. I get the length before "Line 1 of 3" so I know where to start removing from.
3. I will try this.
Thanks for your help!
|
|
|
|
|
asquaredtech wrote: I understand this, I was hoping there was a way to count the "\"s.
Why? What purpose does it serve?
asquaredtech wrote: The remove function allows me to remove the string "Line 1 of 3" from the text box, then append the string "Line 2 of 3" which is returned from method I call. I get the length before "Line 1 of 3" so I know where to start removing from.
No, it doesn't. The string returned by the Text property is still immutable (it does not, and cannot, change). Since you don't assign the new string you've created with the Remove method back to the Text property then nothing gets updated.
|
|
|
|
|
asquaredtech wrote: I was hoping there was a way to count the "\"s.
This is non-sense, there are no backslashes in your text, they are only present in your C# source code
to tell the compiler it has to treat the next character differently, hence:
"r" is one character, and "\r" is also one character albeit a different one.
In memory at run-time they both occupy the same number of bytes, and String.Length will return 1 for each of them.
asquaredtech wrote: The remove function allows me to remove the string "Line 1 of 3"
Only if you store the return value of that method, it does NOT alter its parameters in some magical way.
|
|
|
|
|
asquaredtech wrote: textBoxConsole.Text.Remove(len - 1);
instead of this try :
textBoxConsole.Text = textBoxConsole.Text.Replace("\r\n","");
if you just wants to remove the "NewLine Char"
|
|
|
|
|
Just in case you have not yet realised that the '\'s are not really in your text.
Try to think of it like this:
If you were writing to your mum about something Fred had said to you. You would write something like
and then Fred said "How long are you staying?"
now Fred did not say open-quote blah blah ... question mark close-quote
Those are punctuation marks and are used to indicate to the reader, that the words were spoken by someone and that they were asking a question.
In C# the compiler is the reader and the '\'s are punctuation marks, except we call them 'escape characters' and in C# they indicate a non-printable character is coming. In the case of '\' the compiler says to itself
"Ah, an escape character. I must treat the next character specially because it is not printable."
It doesn't do anything with the '\'s, it throws them away. So by the time your "Sending File....\r\n" reaches the TextBox it is only 18 characters long.
In the TextBoxs' Text property it is stored in memory as a series of numbers:
S e n d i n g F i l e . . . .
83, 101, 110, 100, 105, 110, 103, 32, 70, 105, 108, 101, 46, 46, 46, 46, 13, 10
32 is the number for space character.
13 is the character for Return
10 is the character for New Line.
I hope this helps you.
Henry Minute
If you open a can of worms, any viable solution *MUST* involve a larger can.
|
|
|
|
|
Henry Minute wrote: Just in case you have not yet realised that the '\'s are not really in your text.
Try to think of it like this:
If you were writing to your mum about something Fred had said to you. You would write something like
and then Fred said "How long are you staying?"
now Fred did not say open-quote blah blah ... question mark close-quote
Those are punctuation marks and are used to indicate to the reader, that the words were spoken by someone and that they were asking a question.
That is a really good analogy. I'll have to remember that.
|
|
|
|
|
I just hope asquaredtech got-it too
Henry Minute
If you open a can of worms, any viable solution *MUST* involve a larger can.
|
|
|
|
|
what is this? programming 101. that was understood. look at the real problem, not what you think is the problem. You might as well told me what a modulo operator is.
Some programmers are so arrogant.
|
|
|
|
|
I am sorry that I was not able to help you.
None of what I, or others here wrote or thought was arrogance.
We were simply trying to find a way to help you.
Some of the statements you made in describing your problem, were simply incorrect and were the type of misconceptions that new programmers hold. I therefore tried to answer in the simplest terms I could, not to make you feel small, simply to try to find a level of understanding. In order to help you.
Unfortunately the code you posted in your OP did not do what you said that it would. This meant that nobody was able to understand what your problem really was.
Once again, sorry that I was not able to help.
Henry Minute
If you open a can of worms, any viable solution *MUST* involve a larger can.
|
|
|
|
|
Henry Minute wrote: 83, 101, 110, 100, 105, 110, 103, 32, 70, 105, 108, 101, 46, 46, 46, 46, 13, 10
Seems like you have one of those rare ASCII TextBoxes, mine all speak Unicode
|
|
|
|
|
Shhh! Don't confuse things.
Henry Minute
If you open a can of worms, any viable solution *MUST* involve a larger can.
|
|
|
|
|
Everybody,
It was well understood before this was posted that the "\r\n" is an escape sequence and the "\r" is compiled as one character.
My question is why the the index of the string counts the "\"s and the length does not.
I am very surprised that there is not an easy way to do this. All I want to do is overwrite an existing string of text with a new string of text. I have it working other than the fact that the string counts the "\"s when referring to the index of the string.
|
|
|
|
|
asquaredtech wrote: string counts the "\"s when referring to the index
I am pretty sure that it doesn't. You are aware that the index of the first character in a string is 1?
It cannot, repeat cannot count the '\'s because they are not repeat not there!
Henry Minute
If you open a can of worms, any viable solution *MUST* involve a larger can.
|
|
|
|
|
I have to admire your tenacity.
|
|
|
|
|
Henry Minute wrote: You are aware that the index of the first character in a string is 1?
I hope he's not, as the index of the first character in a string is 0.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I don't know how that happened, I distinctly remember typing a 0.
At least I remember thinking I should type a 0, it was late, I was tired. What can I say.
Henry Minute
If you open a can of worms, any viable solution *MUST* involve a larger can.
|
|
|
|
|