Results 1 to 12 of 12
- 08-13-2011, 08:58 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 3
- Rep Power
- 0
how to compare one string with another dynamic length pattern string?
Hi, I want to compare one string (str1) with another string (str2) pattern. Actually str1 is the input string and str2 is the pattern of the string. This pattern contains only and only ‘R’ character. No of ‘R’ character is not predefined here. More precisely, str2 can be sometime RR, str2 can be sometime RRRRRRR. But str2 can never contain any character except ‘R’.
Now I can do it, by using a for(int i=0;i<str2.length(),i++)if(str1.charAt[i].equals(str2.charAt[i])) but this method is time consuming, as I am scanning each character in both strings. Please help. Can regular expression work here?
Thank you.
- 08-13-2011, 09:17 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Are you saying that you are trying to see whether a string consists of precisely some given number of repetitions of the character 'R'? In that case you could use regular expression quantifiers. [edit] rereading your post since you already have str2 just use the equals() method of String to see if str1 consists of the same number of 'R' characters.
Last edited by pbrockway2; 08-13-2011 at 09:23 AM.
- 08-13-2011, 09:23 AM #3
Member
- Join Date
- Aug 2011
- Posts
- 3
- Rep Power
- 0
string2 can only contain R (it can't contain any other character) but no of character which string2 contains, is run time changing. i just want to check that whether input string is equal to string2(which contains only some number of R) .
- 08-13-2011, 10:51 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Maybe you can do something like splitting the string, and if the array length is greater than 1 you know you had at least one pattern match.
- 08-13-2011, 12:41 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Don't use regular expressions for this; they're overkill. For a pattern P with n identical characters c find the leftmost position of this character c in the String str. Next check whether or not n-1 characters c are present in the String. If the search fails at position j find a leftmost character c starting at position j+1. This is a simplified 'failure function' also present in the KMP pattern finding algorithm and finds a pattern in a String in O(m) where m is the length of the String. Also see this link.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-13-2011, 01:30 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Surely it's a case of are they equal in length, if so then simply running through str1 to check all the characters are 'R'? (I've become a pirate, Arrrrr)
Or am I missing some trick here?
- 08-13-2011, 01:46 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-13-2011, 01:48 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
There isn't a pattern, though.
There's one string with a load of Rs in it, and there's the input string that the OP wants to compare to the load of Rs.
In other words, does the input (str1) contain the same number of Rs (and nothing else) as the other string (str2).
That barely counts as a pattern...
- 08-13-2011, 01:52 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
- 08-13-2011, 01:56 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
That's not the way I read it:
"i just want to check that whether input string is equal to string2(which contains only some number of R) . "
Which is a case of "are they the same length and does str1 only contain Rs".
It doesn't say to me that str1 will have a slab of Rs in the middle of some other characters...
In fact (and it's Saturday so forgive my slowness) but why not simply str2.equals(str1)?
- 08-13-2011, 02:20 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-15-2011, 10:12 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
Why we use String compare to String Buffer
By Sharath_Forums in forum New To JavaReplies: 1Last Post: 12-06-2010, 06:21 AM -
String.length?
By rizowski in forum New To JavaReplies: 7Last Post: 11-20-2010, 05:33 PM -
string length HELP PLZ!!
By vette427 in forum EclipseReplies: 1Last Post: 09-23-2010, 06:12 AM -
Validating for length of String
By Bascotie in forum New To JavaReplies: 1Last Post: 10-11-2009, 11:45 AM -
Comparing string using == or != (how to compare string in if else)
By fiqueudrue in forum New To JavaReplies: 6Last Post: 02-10-2009, 06:48 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks