开发者

string length in java

String h;
h=(String) Tok.nextElement();
System.out.println("" + ++n +": "+h);
h.trim();   
System.out.println("The length of h :"+n+h.length());
wordlist[now]=h;
System.out.println("Now length is"+wordlist[now].length());
                                                     

This "\u0B9A\u0BBF\u0BAF\u0BA9" is the string assigne开发者_StackOverflow社区d to h.The output is

The length of h :128

Now length is 41

I'm totally confused :( help please. Sorry . the pblm is not with operator but with length also note that the length of h is 128 !! where as the actual length is 28


wordlist[now++]=h assigns h to wordlist[now], then increases now, after which wordlist[now] is most likely not h (unless is already was before).

You may want to use wordlist[++now]=h instead. Better yet, avoid using incrementors within statements altogether.


I think this is the problem of your String concat.

System.out.println("The length of h :"+n+h.length());

The h's length is really 28. However You put var "n" before that, where I believe the value of n was 1.

Since you concat it with String, instead of sum n and h, it will just concat n and h as String.

So n+h.length = "1"+"28" = "128"

In other words, please change your syntax to :

System.out.println("The length of h :"+h.length());

I also want to comment this syntax:

h.trim();

What is the purpose of having this syntax? If you want to trim variable h, you should assign it back to var h .

h=h.trim();


The discrepancy stems from where/how you use the now++ statement.

First, you're getting the length of the string located at wordlist[now] (pre-increment) and in the subsequent statement, you're getting the length of the string at wordlist[now+1] (post-increment).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜