简体   繁体   中英

Incorrect string length

This may seem a little strange but I have this piece of code which logs my string and its length followed by it in a very simple way:

log.v("something", myString + " -- " + myString.length);

and very oddly what i see in my logview when program runs is:

.1.29 -- 9

im pretty sure my string doesnt have 9 elements here.

I have to say my full string does load from a serial bluetooth device which involves some weird characters but I make sure I bypass them through a function, though I cant filter them all out as it seems here.

Why do I see this contradiction in my string variables? and how would I anticipate that

Possible errors:

  1. You didn't specify what's in myString (at least in the code above).
  2. You have myString but the length is from mString .

These could be the possible causes of error.

String length is 9 in your case. You are doing concatenation of two Strings and then write it's length. log.v("something", myString + " -- " + myString.length); where myString+' -- ' has the length of myString.length()+ 4 , so You have .1.29 which length is 5 and ' -- ' which length is 4 and that is 9

您是否尝试过删除可能的空格?

  myString.trim().length

Probably some characters are not being outputted in the logger for some reason. Try this:

  for(int i=0; i<myString.length();i++)
  {
     log.v("something", "character number " + i + ": " + myString.charAt(i));
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM