Compare string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • webmaniac

    Compare string

    Hi,

    does anyone know, How to compare characters in 2 string & return How
    many characters match in both String.
    Like the First string is:
    "Hello World"
    And second is "This is a Word"
    So the Function returns 3 Characters "Wor" is same in both.
  • Martin Rinehart

    #2
    Re: Compare string

    Try two boolean arrays. Initialize both to 26 'false' values. March
    through the characters in the first string, setting 'true' the
    appropriate array elements. Ditto for second array/second string.
    'And' the two arrays. Count the 'true' values.

    Comment

    • webmaniac

      #3
      Re: Compare string

      On Nov 14, 11:29 am, Martin Rinehart <MartinRineh... @gmail.comwrote :
      Try two boolean arrays. Initialize both to 26 'false' values. March
      through the characters in the first string, setting 'true' the
      appropriate array elements. Ditto for second array/second string.
      'And' the two arrays. Count the 'true' values.
      Hi Martin,

      Can you provide some example?

      Thanks

      Comment

      • Janwillem Borleffs

        #4
        Re: Compare string

        webmaniac schreef:
        does anyone know, How to compare characters in 2 string & return How
        many characters match in both String.
        Like the First string is:
        "Hello World"
        And second is "This is a Word"
        So the Function returns 3 Characters "Wor" is same in both.
        var a = "Hello World";
        var b = "This is a Word";
        var c = a.length b.length ? a : b;
        var ref = c == a ? b : a;
        var res = '';

        for (var e = '', i = -1; i < c.length; e = c.charAt(++i)) {
        if (/\s/.test(e)) continue;
        if (ref.indexOf(e) -1) res += e;
        }

        document.write( res); // writes 'Word'


        JW

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Compare string

          Martin Rinehart wrote:
          Try two boolean arrays. Initialize both to 26 'false' values. March
          through the characters in the first string, setting 'true' the
          appropriate array elements. Ditto for second array/second string.
          'And' the two arrays. Count the 'true' values.
          Will produce false negatives because the indexes of the substring in
          question may not match (as here). Will produce false positives because
          matches may not be adjacent.


          PointedEars
          --
          var bugRiddenCrashP ronePieceOfJunk = (
          navigator.userA gent.indexOf('M SIE 5') != -1
          && navigator.userA gent.indexOf('M ac') != -1
          ) // Plone, register_functi on.js:16

          Comment

          • Thomas 'PointedEars' Lahn

            #6
            Re: Compare string

            webmaniac wrote:
            does anyone know, How to compare characters in 2 string & return How
            many characters match in both String.
            Like the First string is:
            "Hello World"
            And second is "This is a Word"
            So the Function returns 3 Characters "Wor" is same in both.
            Sounds like homework.


            PointedEars
            --
            var bugRiddenCrashP ronePieceOfJunk = (
            navigator.userA gent.indexOf('M SIE 5') != -1
            && navigator.userA gent.indexOf('M ac') != -1
            ) // Plone, register_functi on.js:16

            Comment

            • Dr J R Stockton

              #7
              Re: Compare string

              In comp.lang.javas cript message <4d8df00e-cbea-43b1-bf0e-c7b6c59e2f76@w3
              9g2000prb.googl egroups.com>, Fri, 14 Nov 2008 06:23:48, webmaniac
              <sunnyluthra1@g mail.composted:
              >Hi,
              >
              >does anyone know, How to compare characters in 2 string & return How
              >many characters match in both String.
              >Like the First string is:
              >"Hello World"
              >And second is "This is a Word"
              >So the Function returns 3 Characters "Wor" is same in both.
              Here is part of a solution for you to complete; other respondents appear
              not to have understood the question - that was too difficult for
              BigEars.


              function DO(X1, X2) { /* fill this in */ }

              S1 = "Hello banana World"
              S2 = "This is a Word banana "
              for (J1=0, L1=S1.length ; J1<L1 ; J1++)
              for (J2=0, L2=S2.length ; J2<L2 ; J2++)
              DO(S1.substr(J1 ), S2.substr(J2)) ;

              --
              (c) John Stockton, nr London UK. ?@merlyn.demon. co.uk Turnpike v6.05 MIME.
              <URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
              <URL:http://www.merlyn.demo n.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
              <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.

              Comment

              Working...