?
Solved

.shift() equivalent on a text string

Posted on 2014-12-09
3
Medium Priority
?
105 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…
Suggested Courses

741 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