Java Script provides a built-in string object that provides you with many methods for manipulating strings. It does not contain a built-in method to trim leading and trailing blanks from a string. When you need to compare two stings, use the trimString function provided in this article, along with the toUppercase or toLowercase method to pre-process the stings before testing them with the equality operator.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Get The Hottest Natural Health Newsletters Free!

Natural Health Newsletters Free!

[ ] Alternative Medicine
[ ] Anti-Aging
[ ] Aromatherapy
[ ] Back pain Relief
[ ] Herbal Remedies
[ ] Magnetic Therapy
[ ] Natural Nutrition
[ ] Vitamin Research
[ ] Weight Loss

Click here to see all of our newsletters, and sign up for the ones you want!

Join American Consumer Opinion™ and Get Paid Cash!
Get paid $4 to $25 for each survey
Typically $4 to $25 for each survey.
Click Here to join (Available worldwide)

Compare Two Strings

In programming, the word "string" refers to an array of characters like a sentence or a paragraph. You can compare two strings using == (the equality operator) as shown below.

var a = "yesterday";
var b = "Yesterday";

if(a == b)
  alert("The strings are identical");
else
  alert("The strings are different");

When we compare strings this way, we are simply comparing their ASCII or Unicode data as if they were numeric values. This type of comparison is case sensitive. In the example above, a comparison would say that the two strings are different because of the case difference in the first letter. To return a positive result each character in the first string must be the same case as the corresponding character in the second string.

- Java Script provides a built-in string object that provides you with many methods for manipulating strings. Proper coding would declare string variables as shown below.

var a = new String("yesterday");
var b = new String("Yesterday");

But strictly speaking, you don't have to declare the variables to be String objects because when you put the left-value within paracentesis, Java Script automatically stores them in String objects.

But what if we didn't care about case? The string object contains two methods; toUppercase and toLowercase that lets you change the case of all the characters in a string. The code below uses the toUppercase method to change both strings to all uppercase characters before making a comparison.

var strOne = new String("yesterday");
var strTwo = new String("Yesterday");
if(strOne.toUpperCase() == strTwo.toUpperCase())
   alert("The strings are identical");
else
   alert("The strings are different");

In the example above, a comparison would say that the two stings are identical because the toUppercase changed the value of strOne to "YESTERDAY" and the value of strTwo to "YESTERDAY" before the comparison was made.

One common problem with string comparisons, especially when one of the strings was acquired from a form textbox, is blank characters on the beginning or end of a string. The Java Script object does not contain a built-in method to trim leading and trailing blanks from a string.

function trimString(aString) 
{
  while(aString.substring(0,1) == ' ')
  {
    aString = aString.substring(1, aString.length);
  }
  while(aString.substring(aString.length - 1, aString.length) == ' ')
  {
    aString = aString.substring(0,aString.length-1);
  }
  return aString;
}

Web Design Sections

RSS Feed RSS Feed

Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

Get paid cash typically $4 to $25 for each survey you complete!

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269