View Full Version : accessing strings/variable?
scroots
12-25-2002, 06:17 PM
if i have the following ( it may not be technically correct javascript)
exs = "blfdjgjfkdrogjkhtpy"
how can i access each letter of the string or variable or what ever it is?
are there any tutorials on this?
scroots
You can use bracket notation in Netscape, but cross-browser you'll need .charAt(index)
scroots
12-25-2002, 06:32 PM
could you expalin a little more jkd.
thanks.
my main aim is to learn how to access each letter and then write out each letter in turn with a space in between.
its a little project i thought about doing.
scroots
for (var i = 0; i < mystring.length; i++) {
alert(mystring.charAt(i));
}
And I know comparing against .length is wasteful, but performance is negligible for small for loops.
If I wanted to be "true", I'd probably go like:
var len = mystring.length;
for (var i = 0; i < len; i++) {
alert(mystring[i]);
}
(the bracket notation there has to be replaced with .charAt(i) for IE, but that is "truer" due to the way C++ handles strings, or really the lack of a way it handles them)
scroots
12-26-2002, 09:43 PM
i have the follwoing code
<script language="javascript">
<!--
function addtotext(){
// to find x
mystring = "0123456789"
for (var i = 0; i < mystring.length; i++) {
//alert(mystring.charAt(i));
document.write(mystring.charAt(i));
//x =boo
//this.form.mytext.value += x;
}
}
// -->
</script>
how could i add a pause in between the writing of each character? also how could i add text between the character so instead of writting 0123456789 it would write
k0y1a2s3e4r5m6z7q8u9
scroots
whammy
12-27-2002, 12:05 AM
Look at some of my/beetle's recent posts regarding "unique string" or "return uniq;" or something (search of forums), you can just randomly insert some characters (with random numbers that map to ascii codes) within the loop with hardly any modification.
http://www.codingforums.com/showthread.php?s=&threadid=422&highlight=return+uniq
http://www.codingforums.com/showthread.php?s=&threadid=11621&highlight=return+uniq
As for a pause, the only thing in javascript that will do that is the setTimeout() function as far as I know - I'd do a search of the forums to look at the proper syntax, and perhaps google it as well, but it can be a very useful thing to know (especially regarding using it as an object, so you can use clearTimeout() on it later!). :D
scroots
12-27-2002, 07:48 PM
well i have refined (rebodged) my technique but i am still at the same problem.
<script>
var delay=3000 //3 seconds
var nn="ppc" //extra text
var x=new Array(5)
x[0]="a"
x[1]="b"
x[2]="chgtyu"
x[3]="d"
x[4]="e"
//alert(x[0]) // alerts a
//alert(x) // alerts all
function kelm(){
alert(x[0]+nn)
setTimeout("kelm()",delay)
}
</script>
alert(x[0]+nn) alerts appc how can i build a function or use some method so i can alert them all out one at a time so it would alert appc then bppc and so on. (basically recording what array i`m on alerting it and then incrementing it so i can alert the next one)
any ideas?
also what is the precise method of reading a character from an array? or has that all ready been posted?
thanks
scroots
chrismiceli
12-27-2002, 07:53 PM
try this
<script>
test = 0
var delay=3000 //3 seconds
var nn="ppc" //extra text
var x=new Array(5)
x[0]="a"
x[1]="b"
x[2]="chgtyu"
x[3]="d"
x[4]="e"
//alert(x[0]) // alerts a
//alert(x) // alerts all
function kelm(){
alert(x[test]+nn)
++test
setTimeout("kelm()",delay)
}
</script>
if that don't work, you might have to do this
alert(x[top.test]+nn)
scroots
12-27-2002, 07:59 PM
thank you your code works.
all i have to do know is find out how to access each character in an array.
scroots
scroots
12-28-2002, 03:32 PM
i have been very grateful for all your help so dar.
from a little research, i can now write out the first object in an array, does anyone know how to move to the second object and so on in the array.
my code so far (ignore the bits in // they were just my trial and error way of doing things)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>New Page 1</title>
</head>
<body>
<script>
var w=0
var r=0
var test= 0
var outMsg=""
var msg
var i=0
var msgn=0
var delay=3 //0.3 seconds
//var nn="ppc" //extra text
// new line is \n
var x=new Array(5)
x[0]="aytuiopgdxv145*"
x[1]="bafgytuotytyuu*"
x[2]="cdtruiuhrr"
x[3]="d07585754575"
x[4]="epoiytgfdfhgg"
//alert(x[0]) // alerts a
//alert(x) // alerts all
function kelm(){
msg = x[w];
outMsg = outMsg + msg.charAt(i);
i++;
//alert(outMsg)
document.f.k.value=outMsg;
setTimeout("kelm()",delay)
//w++;
//}
//if (msg.charAt(r) == "*") {
//w++;
//kelm();
//}
//alert(msg+nn)
//alert(x[test]+nn)
//++test
//setTimeout("kelm()",delay)
}
</script>
<form method="POST" name="f" action="--WEBBOT-SELF--">
<!--webbot bot="SaveResults" U-File="fpweb:///_private/form_results.txt"
S-Format="TEXT/CSV" S-Label-Fields="TRUE" -->
<p><textarea rows="5" name="k" cols="66"></textarea></p>
<p><input type="button" value="KELM" name="B3" Onclick="kelm()"></p>
</form>
<p> </p>
</body>
</html>
i thought w++; would do it but i get an error.
anyone know the solution
thanks
scroots
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.