Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: how to compare one string with another dynamic length pattern string?

  1. #1
    ravi.josih53 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Thumbs up 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.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.
    ravi.josih53 likes this.

  3. #3
    ravi.josih53 is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default

    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) .

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    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?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    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?
    Not really; you have to go through (almost) the entire String to find the pattern. The only little 'cleverness' is in the next search when a current match failed and the stop criterium; but that isn't rocket science ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    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...

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    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...
    But it's still a pattern ;-) The way I read the question is: given a String s find n consecutive letters 'R', for a given value n.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    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)?

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,022
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    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)?
    Yep, if you're right about the length of those Strings being equal a simple equals( ... ) method can do the job. But then I don't understand why this thread goes on an on ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2014, 08:06 AM
  2. Why we use String compare to String Buffer
    By Sharath_Forums in forum New To Java
    Replies: 1
    Last Post: 12-06-2010, 06:21 AM
  3. String.length?
    By rizowski in forum New To Java
    Replies: 7
    Last Post: 11-20-2010, 05:33 PM
  4. string length HELP PLZ!!
    By vette427 in forum Eclipse
    Replies: 1
    Last Post: 09-23-2010, 06:12 AM
  5. Validating for length of String
    By Bascotie in forum New To Java
    Replies: 1
    Last Post: 10-11-2009, 11:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •