Solved

.shift() equivalent on a text string

Posted on 2014-12-09
3
84 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now