Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Javscript Substrings


Javscript Substrings

By ghostghost | 3764 Reads |
0     0

ok heres how javascript substrings work… an example < script language="JavaScript"> var a1, a2, input; a2="abcdefghij"; a1=a2.substring (2,6) input=prompt("Password:","");

if (input==a1) {

  alert(\"correct you may enter\");

}

else {

  alert:(\"wrong\");

}

}

< /script>

now to explain how it works the first line just tells the server that we are using javascript now… the 2nd line tells it that the variables a1 and a2 and for the input. the 3rd line assigns abcdefghij to the variable a2. the 4th line assigns the substring to a1. the other lines just setup an input prompt, if you type in the correct answer which is store in a1 then you get through if not you get an error

The Substring Explanation syntax:'string'.substring(non-inclusive,value.inclusive value) therefor it basicly is abcdefghij.substring(2,6); now the abcdefghij is the string and the substring will want to take some info from it. It finds out what info it needs to get from the (2,6) it tells it to take the 3rd value from the string notice it says 2 it's none inclusive and it takes all the values up to 6 and that includes the 6th value notice this time it is inclusive so our variable will have the value:- -CDEF-

Hope this helped

Comments
ghost's avatar
ghost 19 years ago

This is a nice article teaching substrings, but i would like to see Lucid_Dream (seeing as he's doing reverse code engineer articles) do a reverse code engineer on this kind of a script because it would be nice to have two different ways of accomplishing this javascript (and it would be a nice introductary lesson to reverse coding).

ghost's avatar
ghost 19 years ago

i made a slight mistake with the syntax it is: 'string'.substring(non-inclusive numeric value,inclusive numeric value) ;)

ghost's avatar
ghost 19 years ago

You can edit the article now if you make a mistake. See it at the top?

ghost's avatar
ghost 19 years ago

npe dont see anything about editing it:|

ghost's avatar
ghost 19 years ago

how will we use + ? i mean s1.substring (6,9)+a2.substring

ghost's avatar
ghost 18 years ago

the reason it is non-inclusive is because the first value is assigned 0