BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 07-11-2007, 02:15 AM   #1 (permalink)
New Member
 
Join Date: Jul 2007
Model: 7100T
PIN: N/A
Carrier: Smart
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help if String contains a string

Please Login to Remove!

It's funny that the java.lang.String's mathches() method isn't included in the JDE. Now, I just need to know if an occurance of a String is in another String like

Code:
String string1 = "bla";
String string2 = "Blackberry";

boolean found = SomeClass.findMatch(string1, string2); //should return true
I have looked at the Using String Pattern Matching to Add Dynamic Menu Items example but it's just too complicated and uses some class that I think I don't need.

Here's what I'm doing right now...

Code:
                Contact contact;
                
                String searchParameter = searchString.toString();
                String contactName;
                int searchStringLength = searchString.length();

                boolean startsWith;
                boolean endsWith;
                boolean inBetween;
                
               
                                                                
                while( allContacts.hasMoreElements() ){
                    contact = (Contact)allContacts.nextElement();
                    
                    contactName = getContactDisplayName(contact).toString();
                    
                    startsWith = contactName.startsWith(searchParameter);
                    endsWith = contactName.endsWith(searchParameter);           
                    inBetween = contactName.indexOf(searchParameter) > 0;
                    
                    
                    /*if searchString is not empty...*/
                    if (searchStringLength > 0){
                        /*add only those who match or contains the searchString*/
                        
                        if (startsWith || endsWith || inBetween){
                            v.addElement(contact);
                        }
                    }                    
                    else{ /*add everything*/
                        v.addElement(contact);
                    }
Thanks!
Offline  
Old 07-11-2007, 08:30 AM   #2 (permalink)
Knows Where the Search Button Is
 
Join Date: Feb 2007
Model: 9000
Carrier: SFR
Posts: 48
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I m not sure I have understand your problem but if it's just about stgring comparison there is some available tools on the api such as indexOf ...
Offline  
Old 07-11-2007, 11:19 AM   #3 (permalink)
New Member
 
Join Date: Jul 2007
Model: 7100T
PIN: N/A
Carrier: Smart
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

problem is that I don't want it to be case sensitive... Right now, it's doing case-sensitive comparison.

Code:
                    contactName = getContactDisplayName(contact).toString();
                    
                    startsWith = contactName.startsWith(searchParameter);
                    endsWith = contactName.endsWith(searchParameter);           
                    inBetween = contactName.indexOf(searchParameter) > 0;
                    
                   
                    /*if searchString is not empty...*/
                    if (searchStringLength > 0){
                        /*add only those who match or contains the searchString*/
                        
                        if (startsWith || endsWith || inBetween){
                            v.addElement(contact);
                        }
                    }                    
                    else{ /*add everything*/
                        v.addElement(contact);
Offline  
Old 07-11-2007, 12:03 PM   #4 (permalink)
New Member
 
Join Date: Jul 2007
Model: 7100T
PIN: N/A
Carrier: Smart
Posts: 5
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi! I was able to form a solution. But it could be nice if someone could optimize it on weather when it's called or in the class itself.

Code:
public final class StringUtils {
    private StringUtils() {    }
   
    public static boolean caseInsensitiveStartsWith(String str1, String str2){
        return str1.toUpperCase().startsWith( str2.toUpperCase() );
    }
   
    public static boolean caseInsensitiveEndsWith(String str1, String str2){
        return str1.toUpperCase().endsWith( str2.toUpperCase() );
    }
   
    public static boolean caseInsensitiveIndexOf(String str1, String str2){
        return str1.toUpperCase().indexOf(str2) > 0;
    }
}
Offline  
Old 07-15-2007, 10:04 AM   #5 (permalink)
Talking BlackBerry Encyclopedia
 
Skipper_Joe's Avatar
 
Join Date: Jan 2007
Location: Kharkov, Ukraine
Model: 8300
Carrier: N/A
Posts: 237
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

you forget to use str2.toUpperCase() in caseInsensitiveIndexOf() method.

I read you code samples and it seems to me that comparison
Code:
if (startsWith || endsWith || inBetween){
    v.addElement(contact);
}
Can be replaced by

Code:
if (inBetween){
    v.addElement(contact);
}
since when startWith or endWith are true, inBetween will always be true too.
Or do I miss something?
Offline  




Copyright 2004-2016 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.