Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
Reply With Quote
  #2 (permalink)  
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
Reply With Quote
  #3 (permalink)  
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
Reply With Quote
  #4 (permalink)  
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
Reply With Quote
  #5 (permalink)  
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
Reply With Quote
  #6 (permalink)  
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

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:47 AM.


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