Solved

.shift() equivalent on a text string

Posted on 2014-12-09
3
93 Views
Last Modified: 2014-12-15
I am looking to loop through a string, focusing on each character at a time.

This is what I want to do, like it is an array:
var cleanText = "this is my string of characters I want to iterate through";
while(cleanText.length){
var currentChar = cleanText.shift();
// do some more stuff
}

Open in new window


so that it concentrates only on that specific character and removes it as it goes.
What alternative can use for this example as it is not an array?
0
Comment
Question by:Al4ddin2
  • 2
3 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40489816
var cleanText = "this is my string of characters I want to iterate through";

arrText = cleanText.split('')

var arrayLength = arrText.length;
for (var i = 0; i < arrayLength; i++) {
    var currentChar = arrText[i];
}

Open in new window

0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 40489940
Actually you don't need to convert the string into an array.
A string is already an array of chars:
var cleanText = "this is my string of characters I want to iterate through";

for(var i = 0; i<cleanText.length; i++){
    var currentChar = cleanText[i];
    console.log(currentChar);
}

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 40489952
Good point.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question