Archive for October, 2007

Handling Strings . 273 str.charAt(i) is the character (Web hosting packages)

Wednesday, October 31st, 2007

Handling Strings . 273 str.charAt(i) is the character for which the loop s blockis currently being executed. The variable i starts at 0, the index of the string s first character, and is incremented each time until it is equal to the index of the string s last character. Suppose the current character is t. The condition looks like this then: list.indexOf(”t”)== 1 If the character t is not found in the string list, the method indexOf() whose argument is t returns 1 exactly the number against which the returned value is tested. If a character is not valid (not found in list), a message is displayed and the function is terminated, indirectly causing the script s execution to end. The function then asks the user to enter the key number, which must be an integer from 1 to 63. Because this is just an example, the input value is not tested. If the user clicks Cancel, the function is terminated. Otherwise, the function continues, and the key number is converted from a numeric string to a number via the parseInt() function. The encoded string, which is returned by the function encode(), is displayed. encode(str, key) At first, an empty string is assigned to the variable code. A loop is used to replace every character of the input string by another character. The index of the current character (the one whose index in str is equal to the loops counter, i)inthe list string is assigned to the variable ind. The bitwise OR operator is given the key number and the value of ind as operands. The character whose index in list is equal to the value returned by the bitwise OR operation is the one used to replace the current character in the new string, so it is assigned to the variable that holds the encoded string. The new string is returned by the function. Summary Strings are a very useful data type in JavaScript. JavaScript tends to organize its elements in the form of objects, so all string-related functions and data are grouped together to form the String object. In this chapter we discussed the methods of the String object, as well as its single property, length. Because strings are direct or indirect (based on the way you create them) instances of this object, you can create prototypes to extend the object s capabilities. The JavaScript example provided at the end of the chapter gives a clear view of the String object s strength. We did not provide many examples in this chapter because string manipulation and handling can be found in almost every example later on in this book. Chapter
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Web server hosting - 272 . Chapter 14 Example 14-1 (ex14-1.htm). A

Wednesday, October 31st, 2007

272 . Chapter 14 Example 14-1 (ex14-1.htm). A simple ciphering script. The output of this script is shown in Figures 14-1 through 14-3. Figure 14-1. A prompt box that requests the string to be ciphered. Figure 14-2. A prompt box that requests a key number. Figure14-3. Thecipheredphrase. . Warning: This is an incredibly primitive encrypting scheme and can easily be broken. It is presented here only as an example of how to use JavaScript and is not meant to be used to encrypt sensitive data. encipher() At first, the encipher() function prompts the user for the string he or she wants to encode. It is stored in the variable str. If the value of the variable is a Boolean false, the function and the script itself are terminated. The motivation behind terminating the function is that a Boolean false value can only be the result of the user pressing Cancel. A for loop is then used to checkthat all characters of the string are also located in the string held by list. The loop iterates through every character of the input string. Take a look at the condition used to test if the character is supported: list.indexOf(str.charAt(i)) == 1
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.

Cpanel web hosting - Handling Strings . 271 Enciphering Chapter

Tuesday, October 30th, 2007

Handling Strings . 271 Enciphering Chapter
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.

270 . Chapter 14 functionToBeCalledIfNotFloat() } The isNaN() (Web hosting colocation)

Tuesday, October 30th, 2007

270 . Chapter 14 functionToBeCalledIfNotFloat() } The isNaN() function is not as important as parseInt and parseFloat, but we have discussed it here for completeness. Evaluating Text Expressions JavaScript supports evaluation and execution of text expressions via the eval() method. Here are some examples: var str=”5+2″ var num = eval(str) alert(num) var al = “alert(’This is an evaluated string.’)” eval(al) This script segment pops up two alerts. The first one displays the number 7, because the expression 5+2 evaluates to 7. The second call to the eval() function does not cause it to return a value, but to execute the statement encapsulated in a string. You can also use the eval() function to convert strings representing numbers to regular numbers. The eval() function accepts any valid JavaScript piece of code in the form of a string. You can store an entire script as a string and then hand it over to this function. The classic example for the function is to let the user enter a mathematical expression in a form field or a prompt box, and to display the result. Here is a simple example: var inp = prompt(”Enter mathematical expression”, “”) alert(inp+”=”+ eval(inp)) String Handling Example In this section we focus on a script that takes advantage of the various built-in functions and other elements of the String object in JavaScript. String Enciphering The following script prompts the user for a short string. It then asks for a numeric key. The key has 63 possible values all integers from 1 to 63. The ciphering technique used in this script is known as XORing, because it is primarily based on the bitwise XOR (exclusive OR) operator. The numeric value of each character of the input string, or password, is mixed with the numeric key value. A reverse process can be used to convert the enciphered string backto the original one. Since the conversion simply swaps the same two characters according to the key, the same JavaScript script is used as the decoder and the encoder. Enough theory let s get to the point! Here is the script:
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Handling Strings . 269 Here are a few (Com web hosting)

Monday, October 29th, 2007

Handling Strings . 269 Here are a few expressions to demonstrate these functions, along with returned values: parseInt(”16″)//16parseInt(”16.33″)//16parseFloat(”16″)//16parseFloat(”16.33″)//16.33parseInt(”Howdy!”)//0 These functions are very useful when accepting input from the user via forms or prompts, because they always return a string, even if it represents a number. Note that both functions return zero when the argument is a Boolean value. Thus, if you want to checkif the user canceled a prompt box by pressing the Cancel button, you must evaluate the condition before parsing the value. Here is an example: Chapter varnum=prompt(”Enteranumberbetween0and9:”) if(num=false) alert(”Youmustenteravaluetocontinue.”) elsenum=parseInt(num) A common but mistaken practice is to parse the value immediately. The result is that you cannot checkif the user canceled the box, because he or she might have entered the number 0, which is parsed to the same value as a Boolean value. Determining if a Value is a Number or Not The isNaN() function evaluates an argument to determine if it is not a number, or NaN. The functions parseFloat() and parseInt() return NaN when they evaluate a value that is not a number or a numeric string. NaN is not a number in any string. If NaN is passed on to arithmetic operations, the result is also NaN. The isNaN() returns a Boolean value, according to the argument. Bear in mind that Internet Explorer 3.0 does not support this feature parseFloat() and parseInt() both return 0 if their argument is neither a string nor a numeric string. However, this is supported in the current version. NaN is not a string, nor is it a data type of its own. It is primarily a number! You can prove that to yourself via the following statement: alert(typeofparseInt(”something”)) The following construct demonstrates how to implement the isNaN function (with the parseFloat() function for the sake of the example): var floatValue = parseFloat(valueToBeConvertedToFloat) if isNaN(floatValue) { functionToBeCalledIfFloat() } else {
Check Tomcat Web Hosting services for best quality webspace to host your web application.

268 . Chapter 14 The script s output is: (Java web server)

Monday, October 29th, 2007

268 . Chapter 14 The script s output is: function foo() { vara=5; alert(a); document.write(”wow”); } String-to-Number Conversion Mathematical operators, for example, accept numeric strings as operands and handle them fine. Here is an example for such an operation: var num1= “7″ var num2 = “2″ var result = num1 num2 document.write(result) This script prints 5, just as if both variables were assigned a plain numeric value rather than a numeric string (we use the term numeric string to characterize a string that encloses a number, such as 911 ). An operation consisting of numeric string operands returns a plain numeric value, not a string. Therefore, you can theoretically convert a numeric string to a number by performing an arithmetical operation on it. If you want, you can even use a function to execute the conversion in the following form: function convert(val) { return val 0 } var num = “911″ num = convert(num) Note that you cannot use the plus (+) operator because it is also a string concatenation operator in JavaScript. If you are not sure whether or not a value is a numeric string or a number, always convert it. It s better to stay on the safe side than to spend hours searching for such errors. Conversion via mathematical operations is somewhat annoying, because it looks like a workaround. Therefore, JavaScript provides us with a few conversion functions, each with its own attributes. parseInt() and parseFloat() These two functions were briefly discussed in Chapter 13, JavaScript Math. They are built-in functions, so they do not belong to any object. They convert their argument from a numeric string to a number. If the argument is a string but not a numeric one, the function returns zero. The parseFloat() function is more general, because it works with floating-point numbers as well as integers. The parseInt() function works with integers only, and returns a rounded-off value when called with a floating- point numeric string. Both functions return the value they are given if it is a plain number, not a numeric string. Therefore, if you are not sure whether a value is a number or a numeric string, simply send it to the function. If a certain value in the script has a chance to be a floating-point number, use parseFloat. It will also workif the value is an integer.
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.

Com web hosting - Handling Strings . 267 However, you can still

Sunday, October 28th, 2007

Handling Strings . 267 However, you can still use any property or method associated with strings on such objects. A more obvious way to convert a number to a string via the constructor function is to assign the new string, or object, to a new variable in the following form: varnum=987varnumericString=newString(num) The toString() Method The toString() method belongs to all objects. Its general syntax is: objectName.toString([radix]) objectName is the object to convert to a string, whereas radix is the base to use for representing numeric values when the calling object is a number. The following example prints the string equivalents of the numbers 0 through 9 in decimal and binary: Chapter for(x=0;x<10;x++) { document.write("Decimal:",x.toString(10),"Binary:",x.toString(2),"
“) } The loop s output is: Decimal: 0 Binary: 0 Decimal: 1 Binary: 1 Decimal: 2 Binary: 10 Decimal: 3 Binary: 11 Decimal: 4 Binary: 100 Decimal: 5 Binary: 101 Decimal: 6 Binary: 110 Decimal: 7 Binary: 111 Decimal: 8 Binary: 1000 Decimal: 9 Binary: 1001 All objects, numbers included, have a toString() method. If an object has no string value, the method returns [object type] , where type is the object type (e.g., Date, Array, Object (user-defined), Image). When used with an array, toString() joins the array elements and returns one string in which elements are separated by commas. This operation is exactly like the join() method which concatenates the elements with a specified delimiter, possibly a comma. For functions, toString() decompiles the function backinto a canonical source string. Take a look at the following script segment: function foo() { vara=5 alert(a) document.write(”wow”) } document.write(foo.toString())
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

266 . Chapter 14 You can also convert (Web hosting rating)

Sunday, October 28th, 2007

266 . Chapter 14 You can also convert the number to a string and assign the numeric string to another variable, or even better, do both operations in one statement: var num = 987 var numericString = num + “” This script results in two different variables; the first holds a pure numeric value and the second, numericString, holds a string type. The side of the variable to which the empty string is concatenated has no importance: var num = 987 var numericString = “” + num If you concatenate several different literals, where some are numbers and others are strings, the expression evaluates to a string. Here is an example: var str = 99 + ” bottles of beer on the wall” However, scripts become tricky when you concatenate more than two values or literals, especially when the first few are numbers. Here is a tricky expression: var str = 50 + 49 + ” bottles of beer on the wall” JavaScript evaluates from left to right. The accumulated value is converted to a string only when a string value or literal is encountered in the expression. In the preceding example, JavaScript adds 50 to 49 in the regular mathematical way, so 50+49 evaluates to 99, which is then concatenated to the string that follows. So the value of str in this case is 99 bottles of beer on the wall . The following statement demonstrates a slightly different situation: var str = “bottles of beer on the wall –”+50+49 Like always, evaluation is done from left to right. The string, “bottles of beer on the wall –”, is concatenated with 50 and evaluates to “bottles of beer on the wall –50″. This value in turn is concatenated with the number 49, and evaluates to “bottles of beer on the wall –5049″, which is certainly not the value we want. A simple workaround is to enclose the numeric operation in parentheses in the following form: var str = “bottles of beer on the wall –”+(50+49) The parentheses instruct JavaScript to evaluate the enclosed expression first, so the value of str in this case is “bottles of beer on the wall –99″. String Instance Construction Another way to convert a number to a string is by providing the number to the String() constructor function, which returns a regular String object. Here is a simple example to demonstrate this: var num = 987 num = new String(num) The data type of the variable num in this case is not a string, but an object. As mentioned earlier, strings created via the String() constructor are regular objects.
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.

Handling Strings (Unable to start debugging on the web server) . 265 Like all methods, you

Sunday, October 28th, 2007

Handling Strings . 265 Like all methods, you can pass it a variable, a property of an existing object, or a plain string literal. The escape() function is not a method associated with any object, but is a part of the language itself. The value returned by the escape function is the string argument, where all nonalphanumeric characters are replaced by a string in the form of %xx , xx being the ASCII encoding of a character in the argument. The unescape() function is responsible for the opposite conversion. That is, it converts the string from nonalphanumeric ASCII encoding to ISO Latin-1 characters. Its syntax is similar: unescape(string) The following example demonstrates the conversion in both directions: varstr1=”Myphone#is123-456-7890″ varstr2=escape(str1) varstr3=unescape(str2) document.write(”Afterescape:”+str2+”
“) document.write(”Afterunescape:”+str3) Chapter The script s output is self-explanatory: After escape: My%20phone%20%23%20is%20123-456-7890 After unescape: My phone # is 123-456-7890 Number-to-String Conversion Occasionally, you need to convert a number to a string. For example, if you want to compute the number of digits in a number, you can convert it to a string and use the length property, which applies only to strings. In this section we take a look at a few ways to convert a number into a string. Empty String Concatenation The most obvious way to convert a number to a string is by concatenating an empty string to the number. Here is an example of such a conversion: varnum=987num+=”" You can also make sure that the value of the variable is a string using the typeof operator in the following way: varnum=987document.write(”numisa”+typeofnum+”
“) num+=”" document.write(”numisa”+typeofnum) The expected output of this script segment is: num is a number num is a string
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.

Web hosting support - 264 . Chapter 14 This method is similar

Saturday, October 27th, 2007

264 . Chapter 14 This method is similar to its equivalent in Perl, the function substr. Nonetheless, it is important to point out the differences. First of all, since Perl does not support objects, the plain substr() function accepts the string itself as the first argument and indexA as the second one. However, the third argument is not indexB, but the length of the substring. Another difference is that when you call the function with a negative value as the offset (indexA), the substring starts that far from the end of the string. In JavaScript, though, a negative index is equivalent to a zero index, the first character of the string. JavaScript prototypes enable us to reproduce the substr function in Perl as a JavaScript method using the following script segment: function substr(offset, length) { if (offset < 0) offset = this.length + offset return this.substring(offset, offset + length) } String.prototype.substr = substr You can use this method with any string in the following way: var str = "abcd" document.write(str.substr( 3, 2)) This statement prints the string bc . escape and unescape JavaScript provides us with some built-in functions that deal with strings, such as escape and unescape. Before we can present these functions, we must discuss the ISO Latin-1 character set. The ISO Latin-1 (8859-1) is the standard set of characters used over the Internet. This standard also serves as the basis for the ANSI character set of MS Windows, but, naturally, Microsoft extended and improved the set. However, only the ISO Latin-1 characters are guaranteed to be supported on a web site. You already know the standard coding scheme of the ISO Latin-1 character set through HTML, which enables you to display a character by its number or name as an entity. For example, the character can be displayed on a page via two different expressions: . © . © The first expression is based on the character code in the ISO Latin-1 character set. The second method is based on the name given to the character. With only a few exceptions, almost all platforms are compatible with the glyphs of ISO Latin-1 (ISO-8859-1). If you are interested in character sets or ISO-8859-1, search the web for more information. The ISO-8859-1 character table can be found in Appendix C. Now backto JavaScript. The escape function returns the ASCII encoding of an argument in the ISO Latin-1 character set. The general syntax is: escape(string)
Searching for affordable and proven webhost to host and run your servlet applications? Go to Linux Web Hosting services and you will find it.