I hate skimming large documents looking for certain keywords or lines of text. Now imagine your boss gives you an assignment to you must find a particular line or word in a JavaScript variable that needs to be replaced and the entire variable and write to an html page. This is some of the many uses for the IndexOf Method.

We used over 10 web hosting companies before we found Server Intellect. They offer dedicated servers, and they now offer cloud hosting!

Let’s begin with the most simple and another “Hello World”, yes I know, you must be overwhelmed with joy; and yes, that is sarcasm for those of you that didn’t pick up what I was putting down. All you will need is an HTML file and it is optional if you would like to use an external JavaScript page as well, I will be to keep the code a bit more readable.

First what we will need to do is establish a string variable that has the value of, drum roll, yes; you guessed it “Hello World.” Once we have done so I will be writing the variable to screen like so:
Code Block
JScript.js
Establishing our String and Write to Page
window.onload = function () {
    var str = "Hello world!";
    document.write(str + "<br/>");
}
Let’s go over what exactly the IndexOf Method actually does and what we need to include in the argument. To use the IndexOf Method we would first need a string to search in, whether this is a word or a large paragraph. Next we will need to then establish what exactly we are searching for, this is a requirement.

Now, what we will find the IndexOf the letter “d” in our variable. Now, what we need to remember that the first position in the string is “0,” not “1”.

We stand behind Server Intellect and their support team. They offer dedicated servers, and they are now offering cloud hosting

Code Block
JScript.js
IndexOf the letter “d”
document.write("IndexOf the letter 'd' in Hello world is : " + str.indexOf("d") + "<br />");
Next we have an optional parameter that we can include if we choose to do so which is the start position of the search. That might seem like a waste of time at first but when you are searching for a variable that might happen to be repeated a number of times in your string. What you do in a situation like this is establish the first IndexOf the variable itself, like we did above in our “Hello World” example.

Then what we will do is take the IndexOf the same Variable but this time we will set the “Start ” parameter to that of the IndexOf the first use plus the length of the variable itself, like so:
Code Block
JScript.js
Multiple Variables
var myString = "This is my new string in which i will repeat the word string one more time.";
    var w = "string";
    document.write(myString + "<br/>");

    document.write("IndexOf the word 'string' in myString is : " + myString.indexOf(w) + "<br/>");
    document.write("IndexOf the second use of the word 'string' in myString is : " + myString.indexOf(w, myString.indexOf(w) + w.length) + "<br/>");
That’s all you need to do. Now you can repeat the same steps of the process if there are more uses of the variable. If you have any questions or are still a bit confused please feel free to download the Source Code, I have included a number of additional examples that will help explain this a bit more. Thank you for joining me and have a nice day.

We used over 10 web hosting companies before we found Server Intellect. Our new server with cloud hosting,was set up in less than 24 hours. We were able to confirm our order over the phone. They responded to our inquiries within an hour. Server Intellect’s customer support and assistance are the best we’ve ever experienced.

JavascriptIndexOfTut.zip