Monday, August 22, 2011

LEARNING JAVASCRIPT - JavaScript Strings (JAVASCRIPT TUTORIAL)

  1. Welcome
    1. Introduction
    2. First Program
  2. Basics
    1. Placing the Code
    2. Lexical Structure
      1. Reserved Words
    3. Variables and Types
      Numbers StringsDatesArrays
    4. Operators
    5. Control Structures
    6. Functions and Objects
    7. Event Handling
    8. Regular Expressions
  3. Advanced Topics
    1. Object Oriented Programming
      1. Constructors and Prototypes
      2. Inheritance
      3. Access Control
    2. Functional Programming in JavaScript
      1. Function Objects
      2. Anonymous Functions
      3. Closures
      4. Higher Order Functions
    3. Modular JavaScript
      1. Modules
      2. Namespaces
    4. Optimization
    5. Metaprogramming
    6. Bookmarklets
  4. Debugging
    1. JavaScript Debuggers
    2. Common Mistakes
    3. Debugging Methods
    4. Browser Bugs
  5. Dynamic Web Client Programming (DHTML)
    1. Runtime Document Manipulation
      1. Introduction to the Document Object Model (DOM)
      2. Finding Elements
      3. Adding Elements
      4. Changing Elements
      5. Changing Element Styles
      6. Removing Elements
    2. Event Handlers
      1. Simple Event Handlers
      2. W3C Event Handlers
      3. Extended Event Handlers
    3. Running Scripts at Page Load
    4. Soul Building Javascript
    5. XML and Javascript
      1. DOM Manipulation
      2. XSL Sprinkles
    6. DHTML Examples
  6. Advanced Web Client Programming
    1. Working with Images
    2. Working with Forms
    3. Working With Cookies
    4. Client-Server Programming (AJAX)
      1. XMLHttpRequest
      2. Handling HTML
      3. Handling JSON
      4. Handling XML
    5. Design Mode
  7. JavaScript Outside HTML
    1. ActionScript in Flash
    2. Adobe PDF Forms
    3. JSOSA for the Macintosh
    4. JScript in Microsoft WSH
  8. Standards and Best Practices
    1. Naming Conventions
    2. Code Structuring
    3. Accessibility
  9. Appendices
    1. Index
    2. Links
    3. Useful Software Tools
    4. History of JavaScript
    5. Contributors


A string is a type of variable which stores a string (chain of characters).

Contents

[hide]

[edit] Basic Use

To make a new string, you can make a variable and give it a value of new String().
var foo = new String();
But, most developers skip that part and use a string literal:
var foo = "my string";
After you have made your string, you can edit it as you like:
foo = "bar"; //foo = "bar"
foo = "barblah"; //foo = "barblah"
foo += "bar"; //foo = "barblahbar"
A string literal is normally delimited by the '  or "  character, and can normally contain almost any character. Common convention differs on whether to use single quotes or double quotes for strings. Some developers are for single quotes (Crockford, Amaram, Sakalos, Michaux) while others are for double quotes (NextApp, Murray, Dojo). Whichever method you choose, try to be consistent in how you apply it.
Due to the delimiters, it's not possible to directly place either the single or double quote within the string when it's used to start or end the string. In order to work around that limitation, you can either switch to the other type of delimiter for that case, or place a backslash before the quote to ensure that it appears within the string:
foo = 'The cat says, "Meow!"';
foo = "The cat says, \"Meow!\"";
foo = "It's \"cold\" today.";
foo = 'It\'s "cold" today.';

[edit] Properties and methods of the String() object

As with all objects, Strings have some methods and properties.

[edit] replace(text, newtext)

The replace() function returns a string with content replaced.
var foo = "microsoft rox";
var newString = foo.replace("rox", "sux")
alert(foo); //microsoft rox
alert(newString); //microsoft sux
As you can see, replace() doesn't actually do anything to the 'foo' object at all.

[edit] toUpperCase()

This function returns the current string in upper case.
var foo = "Hello!";
alert(foo.toUpperCase()); // HELLO!

[edit] toLowerCase()

This function returns the current string in lower case.
var foo = "Hello!";
alert(foo.toLowerCase()); // hello!

[edit] length()

Returns the length as an integer.
var foo = "Hello!";
alert(foo.length); // 6

[edit] substring(start[, end])

Substring extracts characters from the start position
"hello".substring(1); // "ello"
When the end is provided, they are extracted up to but not including the end position.
"hello".substring(1, 3); // "el"

No comments:

Post a Comment