Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2006, 09:46 AM
Authorized User
 
Join Date: Jun 2006
Location: BANGALORE, Karnatak, India.
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ajit
Default 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
__________________
Ajit
 
Old June 24th, 2006, 09:31 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

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
 
Old June 26th, 2006, 10:49 AM
Authorized User
 
Join Date: Jun 2006
Location: BANGALORE, Karnatak, India.
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ajit
Default

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
 
Old June 26th, 2006, 10:54 AM
Authorized User
 
Join Date: Jun 2006
Location: BANGALORE, Karnatak, India.
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to ajit
Default

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
 
Old June 26th, 2006, 11:01 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

No.

Regards,
Rich

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

http://www.catb.org/~esr/faqs/smart-questions.html
 
Old January 20th, 2007, 04:02 PM
Registered User
 
Join Date: Jan 2007
Location: Cape Town, Western Cape, South Africa.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convering a String Array to an Integer array nkrust C# 9 November 17th, 2010 12:02 PM
Casting String array to string Samatha ASP.NET 1.0 and 1.1 Professional 1 December 5th, 2006 07:46 AM
Duplicate Array value/string .NETamateur C# 3 November 3rd, 2006 08:58 AM
String to Array Psion PHP How-To 1 September 5th, 2006 11:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.