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
Contributor
lostdragon
Posts: 34
Registered: ‎10-03-2012
My Device: 9300
My Carrier: cell c
Accepted Solution

string pattern

Hi
I want to match a pattern in a string. Unfortunately I have nt been able to find any examples which show how to use the stringpattern class. Can anyone plz show give brief description of how to match a pattern in a string. Thnk u in advance
Please use plain text.
Developer
peter_strange
Posts: 19,602
Registered: ‎07-14-2008
My Device: Not Specified

Re: string pattern

[ Edited ]

What are you trying to do - match to verify the String or match to provide some processing option?

Please use plain text.
Contributor
lostdragon
Posts: 34
Registered: ‎10-03-2012
My Device: 9300
My Carrier: cell c

Re: string pattern

Example if I have a string1 harajukuktcgh. And the user enters string2 ajuku. I want to check if string1 contains string2 pattern.
Please use plain text.
Developer
alishaik786
Posts: 285
Registered: ‎08-26-2011
My Device: 9900
My Carrier: Not Specified

Re: string pattern

This may helps you:

       public boolean isEqualToString(String source, String pattern)
	{    
	    if (source == null || pattern==null)
	    	return false;
	    if(source.length()==0||pattern.length()==0||source.length()<pattern.length())
	    	return false;
	    
	    int index=0;
	    while(index<source.length())
	    {
	    	if(source.charAt(index)==pattern.charAt(0))
	    	{
	    		String str=source.substring(index,index+pattern.length());
	    		if(str.equals(pattern))//If you don't want case sensitive then use "str.equalsIgnoreCase(pattern)"
	    		{
	    			System.out.println("=========Equal String ==============: "+str);
	    			return true;
	    		}
	    	}
	    	index++;
	    }
	    return false;
	}
	
//Example:  boolean isContainsString=isEqualToString("Ali shaik ali", "ali");

 

Try this and let me know.

 

 

=================================================================================
Feel free to click LIKE button if the solution helps you;
--
Regards,

ALI SHAIK.
Please use plain text.
Contributor
lostdragon
Posts: 34
Registered: ‎10-03-2012
My Device: 9300
My Carrier: cell c

Re: string pattern

Thank u this should work. Blackberry has a net.rim.device.api.util.StringPattern; class do u knw how to use it?
Please use plain text.
Developer
alishaik786
Posts: 285
Registered: ‎08-26-2011
My Device: 9900
My Carrier: Not Specified

Re: string pattern

[ Edited ]

 

lostdragon

First of all thanks for giving this type of suggesstion to find the solution in "less coding"

 

As you mentioned, we can get by using StringMatch class

 

//Example: boolean isMatch=hasString("Ali shaik ali", "ali");

public boolean hasString(String name, String pattern) { // new StringMatch(patternToMatch, caseSensitivity, spaceDelimitsNewPattern); StringMatch match=new StringMatch(pattern,true,true); if(match.indexOf(name)!=-1) { System.out.println("=======String: "+name+"=======has====== "+pattern+" : "+match.numStringsInPattern()+" times"); return true; } else return false; }

 

 Try this and Let me know.

 

 

 

=================================================================================
Feel free to click LIKE button if the solution helps you;
--
Regards,

ALI SHAIK.
Please use plain text.
Contributor
lostdragon
Posts: 34
Registered: ‎10-03-2012
My Device: 9300
My Carrier: cell c

Re: string pattern

Thank u
Please use plain text.