matching first 3 characters

M

Matt L.

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(filename, myitemtext) {
var valu = filename;
Str=valu;
Str=Str.substring(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("Activities - First Floor");
else if(valu.search( "a2" ) > -1)
alert("Activities - Second Floor");
else if(valu.search( "a3" ) > -1)
alert("Activities - Third Floor");
else if(valu.indexOf("b")!=-1)
alert("Business");
else if(valu.indexOf("dc")!=-1)
alert("Daycare");
}
 
R

Robert

Matt said:
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)?

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

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

2. What's the difference between 'indexOf' and 'search'? (I left both in the
code below)

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.
3. What function does the operator and following number perform in the 'if'
statements? (I left both operators in the code below)

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

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.
The
code below crudely works, but only if I move "Activities" below "Fine
Arts"

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.indexOf("b") != 1)
alert("Found it!");
else if (filename.indexOf("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(filename) {
var first3Chars = filename.substr(0, 3);
switch (first3Chars) {
case "hh" : // do something
break;
case "fa1" : // do something
break;
}
}
 
A

alu

Matt L. said:
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(filename, myitemtext) {
var valu = filename;
Str=valu;
Str=Str.substring(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("Activities - First Floor");
else if(valu.search( "a2" ) > -1)
alert("Activities - Second Floor");
else if(valu.search( "a3" ) > -1)
alert("Activities - Third Floor");
else if(valu.indexOf("b")!=-1)
alert("Business");
else if(valu.indexOf("dc")!=-1)
alert("Daycare");
}


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("hhw") would result in 'not found'


function searchResult(filename) {
var firstThree = filename.substring(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
 
M

Matt L.

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

Dr John Stockton

JRS: In article <HvKdnZ2dnZ3aVd7fnZ2dnSRxmN6dnZ2dRVn-
(e-mail address removed)>, dated Fri, 19 Aug 2005 09:29:13, seen in
news:comp.lang.javascript said:
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.

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

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

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

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

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.indexOf(x)) return s
return "Eh?" }

alert(sR(NameOfFile))


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

ASM

Matt said:
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)

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

url = url.search; // extract what follow url (?name=Smith&surname=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.search.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);
3. What function does the operator and following number perform in the 'if'
statements? (I left both operators in the code below)

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

cf above about switch
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top