Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #16066 > unrolled thread
| Started by | justaguy <lichunshen84@gmail.com> |
|---|---|
| First post | 2012-09-20 16:25 -0700 |
| Last post | 2012-09-22 19:18 +0100 |
| Articles | 6 — 4 participants |
Back to article view | Back to comp.lang.javascript
indexOf fails to find the first letter in a string justaguy <lichunshen84@gmail.com> - 2012-09-20 16:25 -0700
Re: indexOf fails to find the first letter in a string "S.T." <anon@anon.com> - 2012-09-20 17:16 -0700
Re: indexOf fails to find the first letter in a string justaguy <lichunshen84@gmail.com> - 2012-09-20 18:42 -0700
Re: indexOf fails to find the first letter in a string Scott Sauyet <scott.sauyet@gmail.com> - 2012-09-20 17:32 -0700
Re: indexOf fails to find the first letter in a string justaguy <lichunshen84@gmail.com> - 2012-09-20 18:42 -0700
Re: indexOf fails to find the first letter in a string Dr J R Stockton <reply1237@merlyn.demon.co.uk.invalid> - 2012-09-22 19:18 +0100
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2012-09-20 16:25 -0700 |
| Subject | indexOf fails to find the first letter in a string |
| Message-ID | <7c3f6696-0634-4b17-9ac6-720ca1572e9d@i14g2000yqo.googlegroups.com> |
Sorry to bug you guys, ran a quick Googling for a solution to no
avail, hence, asking it here.
var str = "$100";
// it would work if we declare the var as
// var str = "100$" but no one writes English like that :)
if (str.IndexOf('$')) {
alert("Please remove the $ sign");
return false;
}
What's the correct syntax to do it? Thanks.
[toc] | [next] | [standalone]
| From | "S.T." <anon@anon.com> |
|---|---|
| Date | 2012-09-20 17:16 -0700 |
| Message-ID | <505bb1c2$0$65487$742ec2ed@news.sonic.net> |
| In reply to | #16066 |
On 9/20/2012 4:25 PM, justaguy wrote:
> Sorry to bug you guys, ran a quick Googling for a solution to no
> avail, hence, asking it here.
>
> var str = "$100";
> // it would work if we declare the var as
> // var str = "100$" but no one writes English like that :)
> if (str.IndexOf('$')) {
> alert("Please remove the $ sign");
> return false;
> }
>
> What's the correct syntax to do it? Thanks.
var str = "$100";
if (str.indexOf('$') !=-1) {
alert("Please remove the $ sign");
return false;
}
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2012-09-20 18:42 -0700 |
| Message-ID | <8693c7c7-01a0-431b-8d09-66cfda9042d5@a7g2000yqo.googlegroups.com> |
| In reply to | #16068 |
On Sep 20, 8:16 pm, "S.T." <a...@anon.com> wrote:
> On 9/20/2012 4:25 PM, justaguy wrote:
>
> > Sorry to bug you guys, ran a quick Googling for a solution to no
> > avail, hence, asking it here.
>
> > var str = "$100";
> > // it would work if we declare the var as
> > // var str = "100$" but no one writes English like that :)
> > if (str.IndexOf('$')) {
> > alert("Please remove the $ sign");
> > return false;
> > }
>
> > What's the correct syntax to do it? Thanks.
>
> var str = "$100";
>
> if (str.indexOf('$') !=-1) {
> alert("Please remove the $ sign");
> return false;
>
>
>
>
>
>
>
> }
Thanks.
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2012-09-20 17:32 -0700 |
| Message-ID | <630c662f-4c19-4faf-99c0-8287f9f08077@l14g2000yqo.googlegroups.com> |
| In reply to | #16066 |
justaguy wrote:
> var str = "$100";
> if (str.IndexOf('$')) {
^^^^^^^
wrong capitalization. The name is `indexOf`.
> alert("Please remove the $ sign");
> return false;
> }
> // it would work if we declare the var as
> // var str = "100$" but no one writes English like that :)
That's because `indexOf` returns an integer, and when in integer is
used where a boolean is expected, as in the `if` statement above, a
zero is read as `false`. Since the first character of the string has
index 0, the above is interpreted as `if (0)`, or equivalently, `if
(false)`.
A safer solution would be to check
if (str.indexOf("$") > -1) {
// ...
}
-- Scott
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2012-09-20 18:42 -0700 |
| Message-ID | <9e32492e-9d9f-4750-a2ce-71814b5ed287@j14g2000yqb.googlegroups.com> |
| In reply to | #16070 |
On Sep 20, 8:32 pm, Scott Sauyet <scott.sau...@gmail.com> wrote:
> justaguy wrote:
> > var str = "$100";
> > if (str.IndexOf('$')) {
>
> ^^^^^^^
>
> wrong capitalization. The name is `indexOf`.
>
> > alert("Please remove the $ sign");
> > return false;
> > }
> > // it would work if we declare the var as
> > // var str = "100$" but no one writes English like that :)
>
> That's because `indexOf` returns an integer, and when in integer is
> used where a boolean is expected, as in the `if` statement above, a
> zero is read as `false`. Since the first character of the string has
> index 0, the above is interpreted as `if (0)`, or equivalently, `if
> (false)`.
>
> A safer solution would be to check
>
> if (str.indexOf("$") > -1) {
> // ...
> }
>
> -- Scott
Thanks. Ooops, typo.
[toc] | [prev] | [next] | [standalone]
| From | Dr J R Stockton <reply1237@merlyn.demon.co.uk.invalid> |
|---|---|
| Date | 2012-09-22 19:18 +0100 |
| Message-ID | <RymLVnD9DgXQFwi1@invalid.uk.co.demon.merlyn.invalid> |
| In reply to | #16066 |
In comp.lang.javascript message <7c3f6696-0634-4b17-9ac6-720ca1572e9d@i1
4g2000yqo.googlegroups.com>, Thu, 20 Sep 2012 16:25:08, justaguy
<lichunshen84@gmail.com> posted:
>var str = "$100";
>// it would work if we declare the var as
>// var str = "100$" but no one writes English like that :)
>if (str.IndexOf('$')) {
> alert("Please remove the $ sign");
> return false;
>}
>
>What's the correct syntax to do it? Thanks.
var str = "100";
// "100$" but no one writes English like that // false comment
if (/\$/.test(str)) {
alert("Please remove the $ sign");
// return false; // not in a function at present
}
--
(c) John Stockton, nr London, UK. Mail via homepage. Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms and links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web