Bookmark This Site

How to parse strings

How to parse strings

Author: Yasir Osama
Submission Date: 2006-03-03
Website:
Email:

To parse strings in Javascript you need six functions:

substr, split, concat, replace, search, and slice

substr(StartExtraction, ExtractionLength)

-returns the requested string from StartExtraction through ExtractionLength

-StartExtraction is zero based (the index of the first character is 0)

-ExtractionLength is not zero based (if you pass in 5, the function will count 1-5)

-if StartExtraction is a negative number, the function will count backwards starting from the end of the string and loop if necessary (var1=abcdefg, StartExtraction=-2, it will start at e)

Example:

var1 = "abcdefghijk"

var2 = var1.substr(2, 3)

document.write("var2="+ var2 + "
")

Result:

var2=cde

--------------------------------------------------------------------------------

split(Delimiter, MaxNumToSplit)

-takes a string and creates an array out of it

-the delimiter or separator is removed from the returned array

-if the delimiter is left out, the array returned will contain the entire string in one element

Example:

var1 = "one, two, three, four"

var2 = var1.split(",")

document.write("var1=" + var1 + "
");

document.write("var2 has " + var2.length + " elements:
");

for (var i=0; i < var2.length; i++) {

document.write("Array Item #" + i + "=" + var2[i] + "
");

}

Result:

var1=one, two, three, four

var2 has 4 elements:

Array Item #0=one

Array Item #1= two

Array Item #2= three

Array Item #3= four

--------------------------------------------------------------------------------

concat(StringToAdd)

-returns the combined two strings

Example:

var1 = "This house is "

var2 = "old and scary."

document.write("var1 = " + var1 + "
")

document.write("var2 = " + var2 + "
")

document.write("combined = " + var1.concat(var2) + "
")

Result:

var1 = This house is

var2 = old and scary.

combined = This house is old and scary.

--------------------------------------------------------------------------------

replace(ReplaceThis, ReplaceWith)

-returns a new string with ReplaceThis replaced by ReplaceWith.

-this function does not change the string being operated on, it returns a new one, therefore if you want to change the string being operated on you must set that string equal to the change (see below example)

-if you want to replace globally include g in ReplaceThis

-if you want to ignore case include an i in ReplaceThis

-an example of this would be to set var2 = /*/gi;

Example:

var1 = "This*is*my*story!"

var2 = "*"

document.write("BEFORE var1=" + var1 + "
");

var1 = var1.replace(var2, " ")

document.write("AFTER var1=" + var1 + "
");

Result:

BEFORE var1=This*is*my*story!

AFTER var1=This is*my*story!

--------------------------------------------------------------------------------

search(FindThis)

-returns the index (or location) where the string was found

-if FindThis is not found the function will return -1

Example:

var1 = "abcdefg"

var2 = "c"

if (var1.search(var2) > -1) {

document.write("var2 was found in var1");

} else document.write("var2 was not found in var1");

Result:

var2 was found in var1

--------------------------------------------------------------------------------

slice(StartSlice, EndSlice)

-returns the requested string minus what was taken out

-both parameters have a zero based index

Example:

var1 = "abcdefghijk"

var2 = var1.slice(3, 8);

document.write("var2=" + var2 + "
")

Result:

var2=defgh

Rate this article: Lowest = 1 to Highest = 5
View PDF   Bookmark   Printer Friendly

[ ^Top ]    [ Go Back ]