Free web hosts - 114Part IIJavaScript TutorialThe string.indexOf()method returns a number indicating
114Part IIJavaScript TutorialThe string.indexOf()method returns a number indicating the index value (zero based) ofthe character in the larger string where the smaller string begins. The key point about thismethod is that if no match occurs, the returned value is -1. To find out whether the smallerstring is inside, all you need to test is whether the returned value is something other than -1. Two strings are involved with this method: the shorter one and the longer one. The longerstring is the one that appears in the reference to the left of the method name; the shorterstring is inserted as a parameter to the indexOf()method. To demonstrate the method inaction, the following fragment looks to see if the user is running Windows: var isWindows = false; if (navigator.userAgent.indexOf( Win ) != -1) { isWindows = true; } The operator in the ifconstruction s condition (!=) is the inequality operator. You can readit as meaning is not equal to. Extracting copies of characters and substringsTo extract a single character at a known position within a string, use the charAt()method. The parameter of the method is an index number (zero based) of the character to extract. When I say extract, I don t mean delete, but rather grab a snapshot of the character. The origi- nal string is not modified in any way. For example, consider a script in a main window that is capable of inspecting a variable, stringA, in another window that displays map images of different corporate buildings. Whenthe window has a map of Building C in it, the stringAvariable contains Building C. Thebuilding letter is always at the tenth character position of the string (or number 9 in a zero- based counting world), so the script can examine that one character to identify the map cur- rently in that other window: var stringA = Building C ; var bldgLetter = stringA.charAt(9); // result: bldgLetter = C Another method string.substring() enables you to extract a contiguous sequence ofcharacters, provided you know the starting and ending positions of the substring of whichyou want to grab a copy. Importantly, the character at the ending position value is not part ofthe extraction: All applicable characters, up to but not including that character, are part ofthe extraction. The string from which the extraction is made appears to the left of the methodname in the reference. Two parameters specify the starting and ending index values (zerobased) for the start and end positions: var stringA = banana daiquiri ; var excerpt = stringA.substring(2,6); // result: excerpt = nana String manipulation in JavaScript is fairly cumbersome compared to some other scriptinglanguages. Higher-level notions of words, sentences, or paragraphs are completely absent. Therefore, sometimes it takes a bit of scripting with string methods to accomplish whatseems like a simple goal. And yet you can put your knowledge of expression evaluation tothetest as you assemble expressions that utilize heavily nested constructions. For example, the following fragment needs to create a new string that consists of everything from thelarger string except the first word. Assuming the first word of other strings can be of anylength, thesecond statement utilizes the string.indexOf()method to look for the firstspace character and adds 1 to that value to serve as the starting index value for an outer
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services