p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript (http://p2p.wrox.com/forumdisplay.php?f=85)
-   -   String to array (http://p2p.wrox.com/showthread.php?t=44402)

ajit June 23rd, 2006 09:46 AM

String to array
 
I have stored a string value from text box in a var variable.
Now i want to split the string to an array, and want check for each element in that. split method could not work, because there is no delimeter. It is just a word. Example code is,
Code:

var str = document.getElementById("txtName").value;
if str stores "test", i want to store that in an array as 't','e','s','t'
can anybody help me

Ajit

richard.york June 24th, 2006 09:31 PM

It's pretty easy to do, every string has a length property with the information of how long the string is in characters. From that you can use the substring method which takes the position of the string you want. The first argument is the position of the first character within the substring offset from zero, and the second argument is the position of the last character within the substring, also offset from zero.
Code:

var $letters = [];

for (var $i = 1; $i <= $somestring.length; $i++)
{
    $letters[$i] = $somestring.substring(($i - 1), $i);
}

I have to ask, why do you want to do this?? What's the practical application?


HTH!

Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html

ajit June 26th, 2006 10:49 AM

Hi i am new to javascript. I just wanted to remove spacial characters from a string. i wanted to loop through each character in the string and check. Anyway, the problem is solved. Thank's a lot.

Regards,
Ajit

Ajit

ajit June 26th, 2006 10:54 AM

Quote:

quote:Originally posted by ajit
 Hi i am new to javascript. I just wanted to remove spacial characters from a string. i wanted to loop through each character in the string and check. Anyway, the problem is solved. Thank's a lot.

 But you used $ symbol as prefix to those variable names. is there an y spacial reason behind that.
Regards,
Ajit

Ajit
Ajit

richard.york June 26th, 2006 11:01 AM

No.

Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html

myang12 January 20th, 2007 04:02 PM

It's pretty easy to do. And there is a perhaps an easier way.

/* string.split method will split a string into an array based
on the input of the parameter, for e.g. h,e,l,l,o can be put into
an array via: var arr = str.split(","); */

var str = "hello";
/* this will split each individual character into an array */
var arr = str.split("");

/* to output an array into a string, you can use string.join() */
alert(arr.join()); // outputs "hello"
alert(arr.join("-")); // outputs "h-e-l-l-o"

mic



All times are GMT -4. The time now is 03:11 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.