Comparing two strings

Discussion in 'Java' started by HS1, Nov 28, 2004.

  1. HS1

    HS1 Guest

    Hello all

    I want to compare two string to see whether they have a matching pattern or
    not. For example:
    "we are students" will match with "are"
    I tried functions string.matches or string.regionMatches but they did not
    work

    Could you please help
    Thank you
    SH1
     
    HS1, Nov 28, 2004
    #1
    1. Advertising

  2. In article <1101678692.849793@ftpsrv1>, HS1 wrote:
    > Hello all
    >
    > I want to compare two string to see whether they have a matching pattern or
    > not. For example:
    > "we are students" will match with "are"
    > I tried functions string.matches or string.regionMatches but they did not
    > work


    What you are looking for is a substring match, you can accomplish this
    with the indexOf(...) method, e.g.:

    public class T {
    public static void main(String args[])
    {
    String s1 = "we are students";
    String s2 = "are";
    int startIndex = s1.indexOf(s2);
    if (startIndex > 0) {
    System.out.println("Found substring at: " + startIndex);
    }
    else {
    System.out.println("Found no match");
    }
    }
    }

    Regards,
    --
    Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
     
    Rob van der Leek, Nov 28, 2004
    #2
    1. Advertising

  3. HS1

    MaSTeR Guest

    > public class T {
    > public static void main(String args[])
    > {
    > String s1 = "we are students";
    > String s2 = "are";
    > int startIndex = s1.indexOf(s2);
    > if (startIndex > 0) {
    > System.out.println("Found substring at: " + startIndex);
    > }
    > else {
    > System.out.println("Found no match");
    > }
    > }
    > }
    >


    Actually
    if (startIndex != -1) {
    System.out.println("Found substring at: " + startIndex);
    }
    else {
    System.out.println("Found no match");
    }
    }
    }

    Otherwise wouldn't find a match for
    String s1 = "we are students";
    String s2 = "we";
     
    MaSTeR, Nov 29, 2004
    #3
  4. In article <>, MaSTeR wrote:
    >> public class T {
    >> public static void main(String args[])
    >> {
    >> String s1 = "we are students";
    >> String s2 = "are";
    >> int startIndex = s1.indexOf(s2);
    >> if (startIndex > 0) {
    >> System.out.println("Found substring at: " + startIndex);
    >> }
    >> else {
    >> System.out.println("Found no match");
    >> }
    >> }
    >> }
    >>

    >
    > Actually
    > if (startIndex != -1) {
    > System.out.println("Found substring at: " + startIndex);
    > }
    > else {
    > System.out.println("Found no match");
    > }
    > }
    > }
    >
    > Otherwise wouldn't find a match for
    > String s1 = "we are students";
    > String s2 = "we";


    Oops, you're right, actually I always use >= .

    Thanks!

    --
    Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
     
    Rob van der Leek, Nov 29, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. agent349
    Replies:
    5
    Views:
    9,645
    asingh0412
    Mar 10, 2009
  2. ad
    Replies:
    1
    Views:
    733
    Victor Bazarov
    Dec 13, 2004
  3. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    388
    Irrwahn Grausewitz
    Oct 21, 2003
  4. Neroku
    Replies:
    12
    Views:
    564
    Oliver Wong
    Feb 12, 2007
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    772
    Malcolm
    Jun 24, 2006
Loading...

Share This Page