473,386 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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(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
7 1903
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
Hi, I'm fairly new to regular expressions, and this may be a rather dumb question, but so far I haven't found the answer in any tutorial or reference yet... If I have f.i. the string "The...
8
by: Synonymous | last post by:
Hello, Can regular expressions compare file names to one another. It seems RE can only compare with input i give it, while I want it to compare amongst itself and give me matches if the first x...
1
by: Antony Paul | last post by:
Hi all, What are the characters used for pattern matching with PostgreSQL 7.3. I know it is using % and _ . Any other characters ?. rgds Atnony Paul ---------------------------(end of...
8
by: John Hazen | last post by:
I want to match one or two instances of a pattern in a string. According to the docs for the 're' module ( http://python.org/doc/current/lib/re-syntax.html ) the '?' qualifier is greedy by...
15
by: Mark Rae | last post by:
Hi, I'm trying to construct a RegEx pattern which will validate a string so that it can contain: only the numerical characters from 0 to 9 i.e. no decimal points, negative signs, exponentials...
3
newnewbie
by: newnewbie | last post by:
I need help with a query where I am trying to match and display the value (First 18 characters of it) in one table that matches the first 18 characters of the field value in another table. Tables are...
7
by: blaine | last post by:
Hey everyone, For the regular expression gurus... I'm trying to write a string matching algorithm for genomic sequences. I'm pulling out Genes from a large genomic pattern, with certain start...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
2
by: lorlarz | last post by:
Looking for feedback on Matching Exercises Maker/ Builder: http://mynichecomputing.com/ReadIt/translateT.html For one thing, I am concerned about storing the matching kwork (known word) as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.