Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > acess of a string first letter.

Reply
Thread Tools

acess of a string first letter.

 
 
Sergio del Amo
Guest
Posts: n/a
 
      11-01-2003
Hi,
I am trying to create a web site to work in Opera 7.2, Explorer 6.0 and
Mozilla 1.4.
I have the code:

var imgs= document.getElementsByTagName("img");
alert(imgs[1].id);

The id of imgs[1] is a10. A window is open with Opera, Explorer and
Mozilla with the text "a10". When i write:

var imgs= document.getElementsByTagName("img");
alert(imgs[1].id[0]);

A window is open in Mozilla with the text "a". But in the other two with
the text "undefined".

Does anybody knows if there is a way to acces the first letter of a
string(in my case the string which contains the id name) with Explorer,
Mozilla and Opera??.
Thanks for your future feedback.

--
Sergio del Amo.

 
Reply With Quote
 
 
 
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      11-01-2003
Sergio del Amo <(E-Mail Removed)> writes:

> Does anybody knows if there is a way to acces the first letter of a
> string


string.charAt(0)
or
string.substring(0,1)
or
string.substr(0,1)

/L
--
Lasse Reichstein Nielsen - http://www.velocityreviews.com/forums/(E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
 
Dr John Stockton
Guest
Posts: n/a
 
      11-02-2003
JRS: In article <(E-Mail Removed)>, seen in
news:comp.lang.javascript, Lasse Reichstein Nielsen <(E-Mail Removed)>
posted at Sat, 1 Nov 2003 19:33:11 :-
>Sergio del Amo <(E-Mail Removed)> writes:
>
>> Does anybody knows if there is a way to acces the first letter of a
>> string

>
> string.charAt(0)
>or
> string.substring(0,1)
>or
> string.substr(0,1)


And for the question as quoted : those access the first character,
assuming that there is one.

S = '123go'
OK = /([a-z])/i.test(S)
RESULT = RegExp.$1

gives me 'g'. A larger search term will be needed for letters outside
A-Z.

What's Result for in JS?!?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-24-2003
Dr John Stockton wrote:

> S = '123go'
> OK = /([a-z])/i.test(S)
> RESULT = RegExp.$1


RegExp.$x is deprecated in JavaScript 1.5. Use RegExp.exec(...) or
String.match(...) and the elements of the resulting array instead:

var
s = "123go",
result = /[a-z]/i.exec(s),
ok = result.length > 0;

> What's Result for in JS?!?


?


PointedEars
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      11-24-2003
JRS: In article <(E-Mail Removed)>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<(E-Mail Removed)> posted at Mon, 24 Nov 2003 04:57:03 :-
>Dr John Stockton wrote:
>
>> S = '123go'
>> OK = /([a-z])/i.test(S)
>> RESULT = RegExp.$1

>
>RegExp.$x is deprecated in JavaScript 1.5. Use RegExp.exec(...) or
>String.match(...) and the elements of the resulting array instead:


RegExp.exec() is buggy, according to Flanagan, in my system. Those for
whom deprecated is inadequate can change it.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-25-2003
Dr John Stockton wrote:

> Thomas 'PointedEars' Lahn
>> RegExp.$x is deprecated in JavaScript 1.5. Use RegExp.exec(...) or
>> String.match(...) and the elements of the resulting array instead:

>
> RegExp.exec() is buggy, according to Flanagan, in my system. [...]


What is the bug, what is your system, and does the same go for
String.match(...)?


PointedEars
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      11-25-2003
JRS: In article <(E-Mail Removed)>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<(E-Mail Removed)> posted at Tue, 25 Nov 2003 13:25:23 :-
>Dr John Stockton wrote:
>
>> Thomas 'PointedEars' Lahn
>>> RegExp.$x is deprecated in JavaScript 1.5. Use RegExp.exec(...) or
>>> String.match(...) and the elements of the resulting array instead:

>>
>> RegExp.exec() is buggy, according to Flanagan, in my system. [...]

>
>What is the bug, what is your system, and does the same go for
>String.match(...)?


If I had had more information, I would (probably) have given it.

Flanagan (ISBN 1-56592-521-1) p.74 says "Buggy in IE 4".

It makes no corresponding remark for String.match

Feel free to present your solution for finding the first letter of a
string, avoiding both buggy and deprecated features.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-26-2003
Dr John Stockton wrote:

> Thomas 'PointedEars' Lahn wrote:
>> Dr John Stockton wrote:
>>> Thomas 'PointedEars' Lahn
>>>> RegExp.$x is deprecated in JavaScript 1.5. Use RegExp.exec(...) or
>>>> String.match(...) and the elements of the resulting array instead:
>>>
>>> RegExp.exec() is buggy, according to Flanagan, in my system. [...]

>> [...]

>
> Flanagan (ISBN 1-56592-521-1) p.74 says "Buggy in IE 4".
>
> It makes no corresponding remark for String.match
>
> Feel free to present your solution for finding the first letter of a
> string, avoiding both buggy and deprecated features.


var
s = "123go",
result = s.match(/[a-z]/i),
ok = result.length > 0;


PointedEars
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      11-26-2003
JRS: In article <(E-Mail Removed)>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<(E-Mail Removed)> posted at Wed, 26 Nov 2003 17:19:52 :-
>> Feel free to present your solution for finding the first letter of a
>> string, avoiding both buggy and deprecated features.

>
> var
> s = "123go",
> result = s.match(/[a-z]/i),
> ok = result.length > 0;


For me, that sets ok to true. So far, so good. But removing the
letters from the string s brings up an Internet explorer script error,
result.length is not an object.

ok = result && result.length > 0 // is a bit better
ok = !!result && result.length > 0 // is satisfactory

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      11-27-2003
Dr John Stockton <(E-Mail Removed)> writes:

> For me, that sets ok to true. So far, so good. But removing the
> letters from the string s brings up an Internet explorer script error,
> result.length is not an object.


Yep. If there is no match, the result of calling the match method is
null.

> ok = result && result.length > 0 // is a bit better
> ok = !!result && result.length > 0 // is satisfactory


The second is only better for stylistic reasons.
If "result" is null, then the first line sets "ok" to null, and the
second sets it to false. Otherwise they are equivalent.

If you plan to use "ok" only in tests, there is no need to convert
it to a boolean. The important thing is that it is a false value.

/L
--
Lasse Reichstein Nielsen - (E-Mail Removed)
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
" *first = new string(*rhs.first);" cannot compile in MSVC fl C++ 10 01-12-2012 02:41 PM
Controling View Wireless Network Acess Points =?Utf-8?B?d2lnZ2xlYnJvdGhlcg==?= Wireless Networking 1 05-10-2005 04:55 PM
Connected on the network can not acess internet =?Utf-8?B?VE9OWVJPTUU=?= Wireless Networking 1 05-04-2005 10:09 PM
How to set up a public acess point yar Wireless Networking 6 11-18-2004 08:32 PM
Adding Wireless acess point to existing network. Shawn Wireless Networking 1 10-17-2004 03:53 PM



Advertisments