We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › how to iterate over a string
Page Index Toggle Pages: 1
how to iterate over a string (Read 1384 times)
how to iterate over a string
May 18th, 2010, 5:39am
 
hello how can i iterate over a string?
im made a algorithms that create random strings like:
jkasjdkslkdaslkdjlsjdlkasjdklsjdljsaldsjdlasdjlkasdjksald

how can i iterate over the characters of a string?

thanks
Re: how to iterate over a string
Reply #1 - May 18th, 2010, 6:15am
 
There are several ways... See String reference for some.
What do you plan to do when iterating
Re: how to iterate over a string
Reply #2 - May 18th, 2010, 11:51am
 
You can even use the java character iterator (which you need to import or fully specify).

Code:

CharacterIterator it = new StringCharacterIterator(myString);
 for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
....................}


It is not everyones taste owing to the syntax but I like it, as PhiLho says it really depends what you want to do.

Re: how to iterate over a string
Reply #3 - May 18th, 2010, 12:16pm
 
With the latest Java 5 syntax friendly version of Processing it can be simplified by using a 'for-each' loop:

Quote:
  String myText = "This is a test";
  
  for (char letter : myText.toCharArray())
  {
    println(letter);
  }

Re: how to iterate over a string
Reply #4 - May 19th, 2010, 3:49am
 
Hey that's really neat!  Smiley
Re: how to iterate over a string
Reply #5 - May 19th, 2010, 4:53am
 
I was also thinking of the toCharArray(), indeed, which is practical but might be costly in memory for very large strings (as it duplicates the data).
I admit I didn't know the CharacterIterator, probably rarely used, but better than using repeatedly substring() and avoiding the duplication to char array.
Re: how to iterate over a string
Reply #6 - May 19th, 2010, 6:07am
 
I'm not so keen about the toCharArray(), given the overhead pointed out by PhiLho (I've been working with some big lsystem Strings myself).

I just liked the syntactic sugar. Another thing about the CharacterIterator, is that If you want to use it with some new text, all you need to do is setText(newText) rather than create a new Iterator object, so I guess it is aimed at efficiency.

Re: how to iterate over a string
Reply #7 - May 19th, 2010, 6:27am
 
I guess it all comes down to the classic trade-off between storage efficiency and processing efficiency. I think strings would have to be pretty big (of order megabytes) for the storage efficiency to become an issue. If they were that big anyway, I'd be looking at exactly why I'd want to iterate over the string in the first place.

Personally, the main efficiency I look for in my code is clarity. Good clear code seems to save a lot of time and is less error prone. Other forms of optimisation come at the end, and only if really necessary. This is why I'm really happy that Processing now incorporates Java 5 syntax.
Page Index Toggle Pages: 1