English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm working on some Java code, and I create a string with characters of another string.

example: strStringName = "" + strAnotherString.charAt( 0 ) + strAnotherString.charAt( 1 );

Since I include the empty quotes at the beginning, this assignment statement should automatically typecast the added characters as string type. This statement works fine for me in my code; this is not where the problem lies.

Next, I evaluate whether that string is equivalent to a string literal in an IF statement.

example:
if (strStringName == "ab")
{
(this doesn't work, blah blah blah)
}

For some reason, this IF statement evaluates the express as "false"...which it shouldn't do!

However, if I use the charAt method, it works...but it sure is a lot of added code I have to write which I shouldn't have to.

example:
if (strStringName.charAt(0) == 'a' && strStringName.charAt(1) == 'b')
{
(this works, blah blah blah)
}

wtf is going on??!!!!...is it a bug with the SDK/JDK?...bleh!

2006-11-19 07:47:21 · 1 answers · asked by entranced82 3 in Computers & Internet Programming & Design

1 answers

use if (string1.equals(string2)) not if (string1 == string2), because the first one looks to see if the strings have the same content, the second one looks to see if the strings share the same memory location.

2006-11-19 07:52:20 · answer #1 · answered by Ben 2 · 4 0

fedest.com, questions and answers