Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 511
Registered: ‎05-31-2009
My Device: Not Specified
Accepted Solution

Can a string look identical but fail .equals or .startsWith?

Hey. I am nearly done on three apps, thanks for those who helped.
I have a string comparison using mystring.startsWith(otherstring). It fails but both strings look identical, I.e. "1555"
When I say "1555".equals("1555") it succeeds. Why is this happening, how to fix tks. J.
Developer
Posts: 511
Registered: ‎05-31-2009
My Device: Not Specified

Re: Can a string look identical but fail .equals or .startsWith?

Hi- using ctrl-click, the one coded recently is made up of bytes, the other is made of individual chars. I never used either, just strings. Java is different in this way.

 

?

 

Cheers

J

Developer
Developer
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

Re: Can a string look identical but fail .equals or .startsWith?

Are you sure there are no whitespace characters at the start?

Developer
Posts: 511
Registered: ‎05-31-2009
My Device: Not Specified

Re: Can a string look identical but fail .equals or .startsWith?

Hey. Yes I stared at it. I've had this problem a long time ago, don't recall how to fix. One string is bytes, the other chars. Weird.

 

J

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Can a string look identical but fail .equals or .startsWith?

Please post the code that reproduces the issue.

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Can a string look identical but fail .equals or .startsWith?


using ctrl-click, the one coded recently is made up of bytes, the other is made of individual chars.

This makes no sense to me. Are these variables declared as String? I think klyubin is right--we need to see the code.

 

To answer the question you asked in the subject: yes, strings that look alike can be unequal. Not only white space, but non-printable characters can cause this. Also, there are many cases where different Unicode characters have the same visual appearance (in some fonts, for instance, upper-case I [U+0049], lower case l [U+006C], and the digit 1[U+0031] all look identical).




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 511
Registered: ‎05-31-2009
My Device: Not Specified

Re: Can a string look identical but fail .equals or .startsWith?

[ Edited ]

Hey. Code is below. I'll post this in case anyone else has had a similar situation with strings and appended chars. It has to do with the RemoveEmptyChar method appending chars... for the record, the values of both sides of the comparison in the last line looked like "1555".

 

RemoveEnyEmptyChar addresses an apparent quirk, where the PhoneCall.getDisplayPhoneNumber() seems to have empty '' chars at the start sometimes. I noticed that it can have more than one, so the code removes one and the string Ctrl-click could still have in the breakdown, 5 chars:   '', '1', '5', '5', '5'. I don't know what this empty char is.

 

Tks for your time, J.

 

_______________________________________________________________________
        String m_phoneNumberCalled; 
            PhoneCall pc = Phone.getActiveCall();
            String displayNumberWithSpaces = pc.getDisplayPhoneNumber();
            m_phoneNumberCalled = RemoveAnyEmptyChar(displayNumberWithoutSpaces);    //// It is a string, but created with appended chars below

_______________________________________________________________________
            
        private String RemoveAnyEmptyChar(String thestring)
        {
            // pc.getDisplayPhoneNumber() sometimes returns a string with a '' char at the start. Remove it if it is there.
            if (thestring.getBytes()[0] == "?".getBytes()[0])
            {
                char[] chars = new char[thestring.length()];
                thestring.getChars(0, thestring.length(), chars, 0);
                
                String s = "";
                for (int i = 1; i < chars.length; i++)
                    s = s + chars[i];
                
                return s;
            }
            else
                return thestring; // No '' to remove
        }



_______________________________________________________________________

 info.setSpecialPhoneNumber(i, ((EditField)specialNumberEditFields.elementAt(i)).getText());
           
_______________________________________________________________________


                        for (int i = 0; i < java.lang.Integer.parseInt(info.getElement(StoreInfo.NUMBEROFSPECIALS)); i++)
                        {
                            // If the phone number matches the special number/prefix, or it is the last special and . (all others) is present (also ignore "")
                            boolean a = m_phoneNumberCalled.startsWith(String.valueOf(info.getSpecialPhoneNumber(i)));
                            boolean aa = m_phoneNumberCalled.equals(String.valueOf(info.getSpecialPhoneNumber(i)));
                            boolean aaa = "1".equals(String.valueOf("1"));
                            boolean aaaa = "1".startsWith(String.valueOf("1"));
                            boolean b = !info.getSpecialPhoneNumber(i).equals("");
                            boolean c = info.getSpecialPhoneNumber(i).equals(".");
                            
                            if ((m_phoneNumberCalled.startsWith(String.valueOf(info.getSpecialPhoneNumber(i))) &&
                                !info.getSpecialPhoneNumber(i).equals("")) || info.getSpecialPhoneNumber(i).equals("."))  
                            {

 

Highlighted
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Can a string look identical but fail .equals or .startsWith?

So, everything's fine: Strings of different length are not equal as far as String.equals is concerned.

 

The underlying problem is that the original String starts with a non-ASCII character which is probably not displayed in the debugger. The overly complicated conversion of the code (RemoveAnyEmptyChars) could've been rewritten as:

 

 

if ((orignalString.length() > 0) && (originalString.charAt(0) > 127)) {
  return originalString.substring(1);
} else {
  return originalString;
}

 

 

Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Can a string look identical but fail .equals or .startsWith?

The first character is not "empty" -- it is some unprintable character. Rather than just blindly strip it, why not find out what it is using something like:

System.out.println( Integer.toHexString( data.charAt(0) ) );

That way, you'll know what you're dealing with. It may be ignorable for the purposes of your application or perhaps not.

 

Internally, Java strings are Unicode characters. When you use String.getBytes(), characters are converted to bytes using the default platform encoding (which is ISO-8859-1 on the BlackBerry). This is usually wrong for characters above 127 (0x7F) and isn't even defined for characters above 255 (0xFF). Much better would be to use String.getBytes("UTF-8"), which gives you a sensible byte encoding.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 656
Registered: ‎02-03-2010
My Device: Z10 LE
My Carrier: Rogers

Re: Can a string look identical but fail .equals or .startsWith?

I use something that may help you to get just the numbers :

 

String justNumbers(String number) {
        String newNumber = "";

        for (int i = 0; i < number.length(); i++) {
            if (number.charAt(i) == '0' || number.charAt(i) == '1'
                    || number.charAt(i) == '2' || number.charAt(i) == '3'
                    || number.charAt(i) == '4' || number.charAt(i) == '5'
                    || number.charAt(i) == '6' || number.charAt(i) == '7'
                    || number.charAt(i) == '8' || number.charAt(i) == '9')
                newNumber = newNumber + number.charAt(i);

        }

        return newNumber;

    }
    


Steven Kader at JaredCo
   Follow me on Twitter    |     Mipixi.com :  Web App for Photo Sharing