Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

this is the 1st time i see this behavior. With this simple code

C#
string strInput = "‭1100100100001111110110101010001000100001011010001100001000110100";            int strlen = strInput.Length;



and a string of 64 chars i get length of 65.

i get real length +1 with every string i put in strInput!!. How can i solve this?
Thanks in advance!

What I have tried:

to change language settings in OS but with no result
Posted
Updated 20-May-16 7:19am
Comments
Alan N 20-May-16 13:10pm    
In the example posted the first character of the string literal is null. I copied and pasted into a hex editor to see it.

You appear to have a non-printable character at the start of your string: e280ad

This is the "Left-to-right override" character:
Unicode Character 'LEFT-TO-RIGHT OVERRIDE' (U+202D)[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-May-16 13:46pm    
Excuse me, where did you get it?
The length of the string is 64. First character is '1'


Oh, I see. I can see it only if I copy that HTML and paste. This is nasty.
Good catch, a 5.

—SA
C#
string strInput = "‭1100100100001111110110101010001000100001011010001100001000110100";
          int len1 = strInput.Length; // 65
          string final = "";
          for (int i = 0; i < strInput.Length; i++)
          {
              char c = strInput[i]; //strInput[0]->'' (8237)
              if (char.IsDigit(c))
                  final += strInput[i].ToString();

          }
          int len2 = final.Length;  //64


The very first char before '1100....' is an Unicode which is non printable char whose unicode value is 8237
Unicode Decimal Code &amp;#8237;[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 20-May-16 13:53pm    
Instead of writing this code, the string literal should be corrected. It's quite nasty to write things like code point 8227 in a literal. Instead, integer constant could be used, or '\u...' literal notation.
—SA
Karthik_Mahalingam 20-May-16 14:00pm    
Hi Sergey, i just explained in code to the OP that, why the string length is 65.
BTW, i didnt get your point, can you give an example on literal notation or post it as a solution.
Karthik_Mahalingam 20-May-16 14:00pm    
Hi Sergey, i just explained in code to the OP that, why the string length is 65.
BTW, i didnt get your point, can you give an example on literal notation or post it as a solution.
Sergey Alexandrovich Kryukov 20-May-16 14:05pm    
It doesn't deserve a solution.
The string literals should not contain anything invisible or otherwise poorly readable.
It if is used, it should be a constant. In worst case, it could be something like
const MyString = '\u8237' + "123"; // or something like that.

But in reality, judging by the inquirer's reaction to the problem, this is purely parasitic character which randomly sneaked in. If the inquirer really needed it, the question would be different. This is just garbage. Your "solution" only suggests to consider the garbage as acceptable.

—SA
Karthik_Mahalingam 20-May-16 14:06pm    
Ok, Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900