Posts

Working with JavaScript Strings: Finding a Substring in a String 5/12

Image
Problem You want to find out if a substring, a particular series of characters, exists in a string. Table of Contents [ Hide ] Solution Use the String object’s built-in indexOf method to find the position of the substring, if it exists: var testValue = "This is the Cookbook's test string"; var subsValue = "Cookbook"; var iValue = testValue.indexOf(subsValue); // returns value of 12, index of substringif (iValue != -1) // succeeds, because substring exists Discussion The String indexOf method returns a number representing the index , or position of the first character of the substring, with 0 being the index position of the first character in the string. To test if the substring doesn’t exist, you can compare the returned value to –1, which is the value returned if the substring isn’t found: if (iValue != -1) // true if substring found The indexOf method takes two parameters: the substring, and an optional second pa-rameter, an

Working with JavaScript Strings: Conditionally Comparing Strings 4/12

Image
Problem You want to compare two strings to see if they’re the same. Table of Contents [ Hide ] Solution Use the equality operator ( == ) within a conditional test: var strName = prompt("What's your name?", ""); if (strName == "Shelley") { alert("Your name is Shelley! Good for you!"); } else { alert("Your name isn't Shelley. Bummer."); } Discussion Two strings can be compared using the equality operator ( == ). When used within a conditional statement, a block of code is run if the test evaluates to true (the strings are equal): if (strName == "Shelley") { alert("Your name is Shelley! Good for you!"); } If the strings are not equal, the first statement following the conditional statement block is processed. If an if ...else conditional statement is used, the block of code following the else keyword is the one that’s processed: if (strName == "Shelley") {

Working with JavaScript Strings: Concatenating a String and Another Data Type 3/12

Image
Problem You want to concatenate a string with another data type, such as a number. Table of Contents [ Hide ] Solution Use the exact same operators, such as addition ( + ) and shorthand assignment ( += ), youuse when concatenating strings: var numValue = 23.45; var total = "And the total is " + numValue; // string has "And the total is 23.45" Discussion A different process occurs when adding a string and another data type. In the case ofanother data type, such as a Boolean or number, the JavaScript engine first convertsthe other data type’s value into a string, and then performs concatenation: // add a boolean to a string var boolValue = true; var strngValue = "The value is " + boolValue; // results in "The value is true" // add a number to a string var numValue = 3.0; strngValue = "The value is " + numValue; // results in "The value is 3" The automatic data conversion also applies if you’re conca

Working with JavaScript Strings: Concatenating Two or More Strings 2/12

Image
Problem You want to merge two or more strings into one. Table of Contents [ Hide ] Solution Concatenate the strings using the addition ( + ) operator: var string1 = "This is a "; var string2 = "test"; var string3 = string1 + string2; // creates a new string with "This is a test" Discussion The addition operator ( + ) is typically used to add numbers together: var newValue = 1 + 3; // result is 4 In JavaScript, though, the addition operator is overloaded , which means it can be usedfor multiple data types, including strings. When used with strings, the results are con-catenated , with the strings later in the equation appended to the end of the string result. You can add two strings: var string3 = string1 + string2; or you can add multiple strings: var string1 = "This"; var string2 = "is"; var string3 = "a"; var string4 = "test"; var stringResult = string1 + " " + stri

Working with JavaScript Strings: Introduction 1/12

Image
JavaScript strings are the most important component of JavaScript, probably used morethan any other data type. Though you may get numeric values from web page forms,the values are retrieved as strings, which you then have to convert into numeric values. Strings are also used as parameters when invoking server-side application calls throughAjax, as well as forming the basic serialization format of every JavaScript object. Oneof the methods that all JavaScript objects share is toString, which returns a stringcontaining the serialized format of the object. Table of Contents [ Hide ] A String Primitive A JavaScript string can be both a primitive data type or an object. As a primitive type,it joins with four other JavaScript primitive types: number, Boolean (true or false), null (no value), and undefined (unknown). In addition, as a primitive data type, stringsare also JavaScript literals : a collection that includes numbers (as either floating pointor integer), the liter