Click to See Complete Forum and Search --> : sorting string values


awebb
06-13-2003, 09:56 AM
I have a menu whose values contain strings of mixed letters and numbers (necessary for the database).
When a user selects an option I want a text box to display a value dependent on part of the string (its the name of a month).
Can you extract just the name of the month from the string?
And if you can is it easily done?

Khalid Ali
06-13-2003, 10:15 AM
since you did not give an example of the string that needs to be parsed..
I can only suggest you toread up on these functions

substring()
indexOf()
chartAt()

http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/string.html#1193137

awebb
06-13-2003, 10:25 AM
Sorry I missed out an example.
They are all dates (but as strings, not dates, if you see what I mean) in the format "Day, Month, 00, 0000".
I just want to be able to populate a text box with a value based on the month bit.
The <option> value and the text are the same format.

Khalid Ali
06-13-2003, 10:31 AM
if the format you mentioned is uniform...then here is how you can get the month out of the string

var temp = "Monday, June, 00, 2003"
var month=(temp.split(","))[1]
alert(month)

SlankenOgen
06-13-2003, 10:34 AM
Create a String array of months *in lower case*-

var MonthArray = new Array("january","february","march"...);
var lgn = MonthArray.length;
var str = "xxxFebruaryzzzzzz";
var i;

function testForMatch()
{

str = str.toLowerCase();

for(i = 0; i < lgn; i++)
{
if(str.match(MonthArray[i]))
{
window.alert(MonthArray[i]);
return;
}
}
}


~mgb

awebb
06-13-2003, 10:45 AM
Thanks for the link, very useful.
It looks like the string.split() method might work well as each part of the string is already separated by commas.
Back to the books!

awebb
06-13-2003, 10:48 AM
Thanks to both of you.

Khalid Ali
06-13-2003, 11:02 AM
You are welcome...
Yep ..split is prety handy function it creates an array of string in relation to the splitting parameter.

SlankenOgen
06-13-2003, 11:53 AM
You're welcome - but watch out for case Sensitivity!

~mgb