SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert string into array

    Hi there,

    Is it possible to break apart a string into characters, be it a word or a sentence, and store each individual character in an array?

    Thanks

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In javascript you can split a string on any delimeter,
    including the empty string, which splits the string between characters-

    string=string.split('');

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Just to clarify, a string can be accessed just as if it were an array.

    Code javascript:
    $char = 'Hello world!'[6] // w
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help everyone.

    Given that a string can be accessed as if it were an array, is it possible to loop through all the characters using a for loop? e.g.:

    for (var i = 0, j = myString.length; i < j; i++) {
    //do stuff to the selected character here
    }
    Last edited by pendleton; Jan 29, 2010 at 07:50. Reason: Submitted it while writing!

  5. #5
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by pendleton View Post
    is it possible to loop through all the characters using a for loop?
    Yes it is, but it's even easier in this case with a while loop.

    Code php:
    $myString = 'Hello world!';
    $i = 0;
    while ($char = $myString[$i++]) {
        echo $char;
    }
    // shows "Hello world!"
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #6
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, that might solve my problem!

    Thanks again for the help.

  7. #7
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeh sorry Dan, took me a little while to get what you meant but I see now.

    Thanks.

  8. #8
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, so I think I have my loop working ok, however each alert comes up saying undefined instead of the letters in sequence. Do I need to declare myString as a new string or am I doing something else wrong?

    Code HTML4Strict:
    <p id="first">This is my string</p>
     
    <script language="JavaScript" type="text/javascript">
    var myString = document.getElementById("first").innerHTML;
    for (var i = 0, j = myString.length; i < j; i++) {
    	alert(myString[i]);
    }
    </script>

    Sorry if this is an easy one but I'm still very new to JS.

    Thanks

  9. #9
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It works fine in my browser, one character at a time in the alert boxes

  10. #10
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm that's odd. I was trying it in IE so I'll give it a go in something else. Thanks for letting me know.

  11. #11
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    It looks like we've come across a technique that works in all modern web browsers, but doesn't in IE.

    You will be safe using this instead:

    Code javascript:
    alert(myString.substring(i, i + 1));
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #12
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, that seems to have solved the issue in IE. Thanks for the solution.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •