By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

matching first 3 characters

P: n/a
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");
}
Aug 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Matt L. wrote:
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;
}
}
Aug 19 '05 #2

P: n/a
alu

"Matt L." <so*****@microsoft.com> wrote
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
Aug 19 '05 #3

P: n/a
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" <ro****@noreply.x> wrote in message
news:43***********************@news.xs4all.nl...
Matt L. wrote:
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;
}
}

Aug 19 '05 #4

P: n/a
Thank you Alu for your prompt help! That is exactly what I was after!
"alu" <no**@none.com> wrote in message
news:RI*******************@news20.bellglobal.com.. .

"Matt L." <so*****@microsoft.com> wrote
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

Aug 19 '05 #5

P: n/a
ASM
Matt L. wrote:
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

--
Stephane Moriaux et son [moins] vieux Mac
Aug 19 '05 #6

P: n/a
JRS: In article <HvKdnZ2dnZ3aVd7fnZ2dnSRxmN6dnZ2dRVn-
0p*****@comcast.com>, dated Fri, 19 Aug 2005 09:29:13, seen in
news:comp.lang.javascript, Matt L. <so*****@microsoft.com> posted :
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".

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

P: n/a
ASM
Dr John Stockton wrote:
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
that did only work with :

if (filename.indexOf(x)) return s
return "Eh?" }

alert(sR(NameOfFile))

my test : alert(sR('Name_a1_OfFile'))
--
Stephane Moriaux et son [moins] vieux Mac
Aug 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.