matching first 3 characters

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

    matching first 3 characters

    In summary, I don't know how/why the following code works and would like to
    know.

    I'm trying to match the first 3 characters of a variable (filename). The
    code below crudely works, but only if I move "Activities " below "Fine Arts",
    implying the code is not matching exactly the first 3 characters. I've left
    the code in my testing state for the following questions:
    1. How do I specifiy the match to include all 3 characters (not just a
    single character match)?
    2. What's the difference between 'indexOf' and 'search'? (I left both in the
    code below)
    3. What function does the operator and following number perform in the 'if'
    statements? (I left both operators in the code below)
    4. Am I on the right track using if/else if? Is this economical?

    Any comments are warmly and eagerly welcome.
    Thanks in advance for your help,
    Matt

    function searchResult(fi lename, myitemtext) {
    var valu = filename;
    Str=valu;
    Str=Str.substri ng(3,Str.length ­-1);
    for (var i = 0; i<Str.length; i++)
    {
    if(valu.indexOf ("hh") !=-1)
    alert("Heritage Hall");
    else if(valu.search( "fa1" ) > -1)
    alert("Fine Arts - First Floor");
    else if(valu.search( "fa2" ) > -1)
    alert("Fine Arts - Second Floor");
    else if(valu.search( "fa3" ) > -1)
    alert("Fine Arts - Third Floor");
    else if(valu.search( "a1" ) > -1)
    alert("Activiti es - First Floor");
    else if(valu.search( "a2" ) > -1)
    alert("Activiti es - Second Floor");
    else if(valu.search( "a3" ) > -1)
    alert("Activiti es - Third Floor");
    else if(valu.indexOf ("b")!=-1)
    alert("Business ");
    else if(valu.indexOf ("dc")!=-1)
    alert("Daycare" );
    }


  • Robert

    #2
    Re: matching first 3 characters

    Matt L. wrote:[color=blue]
    > I'm trying to match the first 3 characters of a variable (filename).
    > 1. How do I specifiy the match to include all 3 characters (not just a
    > single character match)?[/color]

    function startsWith(str, match) {
    return (str.substr(0,m atch.length) == match);
    }

    Or alter this slightly if it really needs to be 3 characters.

    [color=blue]
    > 2. What's the difference between 'indexOf' and 'search'? (I left both in the
    > code below)[/color]

    indexOf is used to find a substring occurance in a string.
    search is used to do a regular expression match in a string.
    See http://www.regular-expressions.info/tutorial.html for information
    about regular expressions.
    [color=blue]
    > 3. What function does the operator and following number perform in the 'if'
    > statements? (I left both operators in the code below)[/color]

    In this case they do the same things, because the regular expressions
    you use are simple literal strings.
    [color=blue]
    > 4. Am I on the right track using if/else if? Is this economical?[/color]

    Well, I see some very weird things.
    Particulary the fact that you create a substring that takes the end of
    the string and then do a for loop for no apparent reason.
    [color=blue]
    > The
    > code below crudely works, but only if I move "Activities " below "Fine
    > Arts"[/color]

    That is because the search for "a1" is not just for the start of the
    string, but anywhere!

    So for example:
    filename = "abc";

    if (filename.index Of("b") != 1)
    alert("Found it!");
    else if (filename.index Of("ab") != 1)
    alert("This will never happen");

    So there can never be an "ab" match if there was no "b" match.

    What you need is probably something like this:

    function searchResult(fi lename) {
    var first3Chars = filename.substr (0, 3);
    switch (first3Chars) {
    case "hh" : // do something
    break;
    case "fa1" : // do something
    break;
    }
    }

    Comment

    • alu

      #3
      Re: matching first 3 characters


      "Matt L." <someone@micros oft.com> wrote[color=blue]
      > In summary, I don't know how/why the following code works and would like[/color]
      to[color=blue]
      > know.
      >
      > I'm trying to match the first 3 characters of a variable (filename). The
      > code below crudely works, but only if I move "Activities " below "Fine[/color]
      Arts",[color=blue]
      > implying the code is not matching exactly the first 3 characters. I've[/color]
      left[color=blue]
      > the code in my testing state for the following questions:
      > 1. How do I specifiy the match to include all 3 characters (not just a
      > single character match)?
      > 2. What's the difference between 'indexOf' and 'search'? (I left both in[/color]
      the[color=blue]
      > code below)
      > 3. What function does the operator and following number perform in the[/color]
      'if'[color=blue]
      > statements? (I left both operators in the code below)
      > 4. Am I on the right track using if/else if? Is this economical?
      >
      > Any comments are warmly and eagerly welcome.
      > Thanks in advance for your help,
      > Matt
      >
      > function searchResult(fi lename, myitemtext) {
      > var valu = filename;
      > Str=valu;
      > Str=Str.substri ng(3,Str.length ­-1);
      > for (var i = 0; i<Str.length; i++)
      > {
      > if(valu.indexOf ("hh") !=-1)
      > alert("Heritage Hall");
      > else if(valu.search( "fa1" ) > -1)
      > alert("Fine Arts - First Floor");
      > else if(valu.search( "fa2" ) > -1)
      > alert("Fine Arts - Second Floor");
      > else if(valu.search( "fa3" ) > -1)
      > alert("Fine Arts - Third Floor");
      > else if(valu.search( "a1" ) > -1)
      > alert("Activiti es - First Floor");
      > else if(valu.search( "a2" ) > -1)
      > alert("Activiti es - Second Floor");
      > else if(valu.search( "a3" ) > -1)
      > alert("Activiti es - Third Floor");
      > else if(valu.indexOf ("b")!=-1)
      > alert("Business ");
      > else if(valu.indexOf ("dc")!=-1)
      > alert("Daycare" );
      > }[/color]


      Hmm, the above loops for no apparent reason and the function is not closed;
      I'm surprised it 'works crudely'.

      You could try using a simple switch, which would take care of the match from
      the start of the string - but note you will have to send a filename that
      exactly matches the cases, or set the cases to exactly match the filename
      because some of your filenames are in fact less than 3 characters long.
      e.g., searchResult("h hw") would result in 'not found'


      function searchResult(fi lename) {
      var firstThree = filename.substr ing(0,3);

      switch (firstThree) {
      case "hh" :
      alert("Heritage Hall");
      break;
      case "fa1" :
      alert("Fine Arts - First Floor");
      break;
      case "fa2" :
      alert("Fine Arts - Second Floor");
      break;
      default :
      alert("not found")
      }
      }

      -alu


      Comment

      • Matt L.

        #4
        Re: matching first 3 characters

        Thanks Robert, the function you provided is exactly what I was looking for.

        Amazing you could wade through my convoluted code and figure out what I was
        trying to do. I will be enforcing a minimum unique 3 character filename
        following the function you submitted.

        The Regular Expression link you provided will also come in very handy.

        I'm well on my way now.

        Thanks again for your prompt help.


        "Robert" <robert@noreply .x> wrote in message
        news:430600b0$0 $11076$e4fe514c @news.xs4all.nl ...[color=blue]
        > Matt L. wrote:[color=green]
        > > I'm trying to match the first 3 characters of a variable (filename).
        > > 1. How do I specifiy the match to include all 3 characters (not just a
        > > single character match)?[/color]
        >
        > function startsWith(str, match) {
        > return (str.substr(0,m atch.length) == match);
        > }
        >
        > Or alter this slightly if it really needs to be 3 characters.
        >
        >[color=green]
        > > 2. What's the difference between 'indexOf' and 'search'? (I left both in[/color][/color]
        the[color=blue][color=green]
        > > code below)[/color]
        >
        > indexOf is used to find a substring occurance in a string.
        > search is used to do a regular expression match in a string.
        > See http://www.regular-expressions.info/tutorial.html for information
        > about regular expressions.
        >[color=green]
        > > 3. What function does the operator and following number perform in the[/color][/color]
        'if'[color=blue][color=green]
        > > statements? (I left both operators in the code below)[/color]
        >
        > In this case they do the same things, because the regular expressions
        > you use are simple literal strings.
        >[color=green]
        > > 4. Am I on the right track using if/else if? Is this economical?[/color]
        >
        > Well, I see some very weird things.
        > Particulary the fact that you create a substring that takes the end of
        > the string and then do a for loop for no apparent reason.
        >[color=green]
        > > The
        > > code below crudely works, but only if I move "Activities " below "Fine
        > > Arts"[/color]
        >
        > That is because the search for "a1" is not just for the start of the
        > string, but anywhere!
        >
        > So for example:
        > filename = "abc";
        >
        > if (filename.index Of("b") != 1)
        > alert("Found it!");
        > else if (filename.index Of("ab") != 1)
        > alert("This will never happen");
        >
        > So there can never be an "ab" match if there was no "b" match.
        >
        > What you need is probably something like this:
        >
        > function searchResult(fi lename) {
        > var first3Chars = filename.substr (0, 3);
        > switch (first3Chars) {
        > case "hh" : // do something
        > break;
        > case "fa1" : // do something
        > break;
        > }
        > }[/color]


        Comment

        • Matt L.

          #5
          Re: matching first 3 characters

          Thank you Alu for your prompt help! That is exactly what I was after!


          "alu" <none@none.co m> wrote in message
          news:RInNe.1325 7$7R.842041@new s20.bellglobal. com...[color=blue]
          >
          > "Matt L." <someone@micros oft.com> wrote[color=green]
          > > In summary, I don't know how/why the following code works and would like[/color]
          > to[color=green]
          > > know.
          > >
          > > I'm trying to match the first 3 characters of a variable (filename). The
          > > code below crudely works, but only if I move "Activities " below "Fine[/color]
          > Arts",[color=green]
          > > implying the code is not matching exactly the first 3 characters. I've[/color]
          > left[color=green]
          > > the code in my testing state for the following questions:
          > > 1. How do I specifiy the match to include all 3 characters (not just a
          > > single character match)?
          > > 2. What's the difference between 'indexOf' and 'search'? (I left both in[/color]
          > the[color=green]
          > > code below)
          > > 3. What function does the operator and following number perform in the[/color]
          > 'if'[color=green]
          > > statements? (I left both operators in the code below)
          > > 4. Am I on the right track using if/else if? Is this economical?
          > >
          > > Any comments are warmly and eagerly welcome.
          > > Thanks in advance for your help,
          > > Matt
          > >
          > > function searchResult(fi lename, myitemtext) {
          > > var valu = filename;
          > > Str=valu;
          > > Str=Str.substri ng(3,Str.length ­-1);
          > > for (var i = 0; i<Str.length; i++)
          > > {
          > > if(valu.indexOf ("hh") !=-1)
          > > alert("Heritage Hall");
          > > else if(valu.search( "fa1" ) > -1)
          > > alert("Fine Arts - First Floor");
          > > else if(valu.search( "fa2" ) > -1)
          > > alert("Fine Arts - Second Floor");
          > > else if(valu.search( "fa3" ) > -1)
          > > alert("Fine Arts - Third Floor");
          > > else if(valu.search( "a1" ) > -1)
          > > alert("Activiti es - First Floor");
          > > else if(valu.search( "a2" ) > -1)
          > > alert("Activiti es - Second Floor");
          > > else if(valu.search( "a3" ) > -1)
          > > alert("Activiti es - Third Floor");
          > > else if(valu.indexOf ("b")!=-1)
          > > alert("Business ");
          > > else if(valu.indexOf ("dc")!=-1)
          > > alert("Daycare" );
          > > }[/color]
          >
          >
          > Hmm, the above loops for no apparent reason and the function is not[/color]
          closed;[color=blue]
          > I'm surprised it 'works crudely'.
          >
          > You could try using a simple switch, which would take care of the match[/color]
          from[color=blue]
          > the start of the string - but note you will have to send a filename that
          > exactly matches the cases, or set the cases to exactly match the filename
          > because some of your filenames are in fact less than 3 characters long.
          > e.g., searchResult("h hw") would result in 'not found'
          >
          >
          > function searchResult(fi lename) {
          > var firstThree = filename.substr ing(0,3);
          >
          > switch (firstThree) {
          > case "hh" :
          > alert("Heritage Hall");
          > break;
          > case "fa1" :
          > alert("Fine Arts - First Floor");
          > break;
          > case "fa2" :
          > alert("Fine Arts - Second Floor");
          > break;
          > default :
          > alert("not found")
          > }
          > }
          >
          > -alu
          >
          >[/color]


          Comment

          • ASM

            #6
            Re: matching first 3 characters

            Matt L. wrote:[color=blue]
            > In summary, I don't know how/why the following code works and would like to
            > know.
            >
            > I'm trying to match the first 3 characters of a variable (filename). The
            > code below crudely works, but only if I move "Activities " below "Fine Arts",
            > implying the code is not matching exactly the first 3 characters. I've left
            > the code in my testing state for the following questions:
            > 1. How do I specifiy the match to include all 3 characters (not just a
            > single character match)?
            > 2. What's the difference between 'indexOf' and 'search'? (I left both in the
            > code below)[/color]

            url = self.location; // get url of displayed page

            url = url.search; // extract what follow url (?name=Smith&su rname=Wil )

            i = url.indexOf('?' ) // gives index of '?' in string url
            i.e :
            truc = 'ste+phane moriaux'
            i = truc.indexOf('+ '); // gives 3 ( i = 3 ) (count begin with 0)
            j = truc.search('+' ); // gives an error
            j = truc.search('e' ); // gives 2 find what's different

            To get what follow url and use it in JS :

            url = self.location; // page's url
            url = url.search; // gives something as : ?store=al
            url = url.substring(1 ) // gives something as : store=al
            url = url.substring(6 ); // gives : al

            Same faster :

            url = self.location.s earch.substring (7);

            var mesg = ''
            switch (url) {
            case 'hh' :
            mesg = 'Heritage Hall';
            break;
            case 'fa1' :
            mesg = 'Fine Arts - First Floor';
            break;
            case 'fa2' :
            mesg = 'Fine Arts - Second Floor';
            break;
            default :
            mesg = 'not found'
            }
            }
            alert(mesg);
            [color=blue]
            > 3. What function does the operator and following number perform in the 'if'
            > statements? (I left both operators in the code below)[/color]

            == : equal (same)
            != : non equal (different)[color=blue]
            > : superior (strictly)
            >= : superior or equal[/color]
            [color=blue]
            > 4. Am I on the right track using if/else if? Is this economical?[/color]

            cf above about switch

            --
            Stephane Moriaux et son [moins] vieux Mac

            Comment

            • Dr John Stockton

              #7
              Re: matching first 3 characters

              JRS: In article <HvKdnZ2dnZ3aVd 7fnZ2dnSRxmN6dn Z2dRVn-
              0p2dnZ0@comcast .com>, dated Fri, 19 Aug 2005 09:29:13, seen in
              news:comp.lang. javascript, Matt L. <someone@micros oft.com> posted :[color=blue]
              >In summary, I don't know how/why the following code works and would like to
              >know.
              >
              >I'm trying to match the first 3 characters of a variable (filename). The
              >code below crudely works, but only if I move "Activities " below "Fine Arts",
              >implying the code is not matching exactly the first 3 characters.[/color]

              You are supplying a number of characters which is not always three, and
              testing for them to appear anywhere. Change the test from > -1 to == 0
              to consider only a match at the beginning.
              [color=blue]
              > I've left
              >the code in my testing state for the following questions:
              >1. How do I specifiy the match to include all 3 characters (not just a
              >single character match)?[/color]

              Difficult when the value of 3 seems to vary.
              [color=blue]
              >2. What's the difference between 'indexOf' and 'search'? (I left both in the
              >code below)[/color]

              indexOf returns a position, and searches for a substring; search is for
              a RegExp argument, but evidently can be used with a plain string.
              [color=blue]
              >3. What function does the operator and following number perform in the 'if'
              >statements? (I left both operators in the code below)[/color]

              If indexOf does not find, it returns -1.
              [color=blue]
              >4. Am I on the right track using if/else if? Is this economical?[/color]

              No.

              Always look out for a means of using a list of entries; it's efficiently
              maintainable. Consider :

              var Tbl = [ {x:"hh", s:"Heritage Hall"},
              {x:"a1", s:"Activities - First Floor"},
              {x:"dc", s:"DayCare"} ]

              function sR(filename) {
              for (var j=0; j<Tbl.length; j++) with (Tbl[j])
              if (!filename.inde xOf(x)) return s
              return "Eh?" }

              alert(sR(NameOf File))


              You will still need to but more specific items, such as "a1", before
              more general ones, such as "a".

              --
              © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
              <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
              <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
              <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

              Comment

              • ASM

                #8
                Re: matching first 3 characters

                Dr John Stockton wrote:[color=blue]
                > Consider :
                >
                > var Tbl = [ {x:"hh", s:"Heritage Hall"},
                > {x:"a1", s:"Activities - First Floor"},
                > {x:"dc", s:"DayCare"} ]
                >
                > function sR(filename) {
                > for (var j=0; j<Tbl.length; j++) with (Tbl[j])
                > if (!filename.inde xOf(x)) return s[/color]

                that did only work with :

                if (filename.index Of(x)) return s
                [color=blue]
                > return "Eh?" }
                >
                > alert(sR(NameOf File))[/color]


                my test : alert(sR('Name_ a1_OfFile'))


                --
                Stephane Moriaux et son [moins] vieux Mac

                Comment

                Working...