Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

.shift() equivalent on a text string

Posted on 2014-12-09
3
Medium Priority
?
107 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

636 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