How can I loop over a string and get each character?
What you are trying to do is iterate over the characters in a string value. In ColdFusion, there are several ways to accomplish this. Some are fairly low level while others are more complex. I will quickly discuss three ways here.
First, as always though, let's just build a string to test with:
Launch code in new window » Download code as text file »
Now, for our testing, we will iterate over each letter and in a bracket, output both the index of the character and the character itself.
Probably the simplest and most commonly used method is to loop over the Length of a string and get the character at a given index:
Launch code in new window » Download code as text file »
This gives us the following output:
[1:O] [2:h] [3: ] [4:m] [5:a] [6:n] [7:!] [8: ] [9:T] [10:h] [11:a] [12:t] [13: ] [14:L] [15:i] [16:b] [17:b] [18:y] [19: ] [20:i] [21:s] [22: ] [23:o] [24:n] [25:e] [26: ] [27:h] [28:o] [29:t] [30: ] [31:m] [32:a] [33:m] [34:m] [35:a] [36:!]
This one takes the complexity up a notch. We are going to convert the string to a byte array where each index holds the byte representation of a single character in the string. Then, instead of looping over the length of the string, we loop over the length of the byte array.
Launch code in new window » Download code as text file »
This gives us the same exact output as the first method. As you can see, even though we are returning a Java byte array for the string (which is traditionally indexed at zero), we can treat it as if it were a standard ColdFusion array and start the index at one. ColdFusion rocks for some nice automatic conversions.
This takes it up one more notch. This creates an instance of the Java string character iterator, java.text.StringCharacterIterator, whose sole purpose in life is to do exactly what you are trying to do. We initialize the iterator with the given string, then we keep asking it for the next character until it is done iterating:
Launch code in new window » Download code as text file »
This gives us "basically" the same output. Sort of. It gives us the same characters but at different indexes:
[0:O] [1:h] [2: ] [3:m] [4:a] [5:n] [6:!] [7: ] [8:T] [9:h] [10:a] [11:t] [12: ] [13:L] [14:i] [15:b] [16:b] [17:y] [18: ] [19:i] [20:s] [21: ] [22:o] [23:n] [24:e] [25: ] [26:h] [27:o] [28:t] [29: ] [30:m] [31:a] [32:m] [33:m] [34:a] [35:!]
As you can see, this starts at index zero, not one. This is because Java in zero based and we are using it in a completely Java way. In the second example we created a Java array but used it like it was a ColdFusion array and hence the automatic index translation. In this case, however, the iteration is done internally to the Java StringCharacterIterator instance and hence, no automatic conversion.
So that's three different ways to iterate over a string. Which one to use? I guess it depends on what you are trying to do? Each of them is going to have pros and cons. I have not done any specific testing, but I can guess that the byte array method (example 2) is the fastest since iterating over an array is always fast. I don't know how the character iterator object does things, so it might be faster or slower than the Mid() methodology. But of course, it depends on how simple or complex you want to be. If it's simple, go with the Mid() methodology... it's just easy. But if you have a ton of data to go through, you might consider the slightly more complicated byte array method.
I, personally, have never used the character iterator in practice. I suppose that you would use it if you need to pass it off to another black boxed algorithm that is expecting something that implements the CharacterIterator interface. But I doubt you are doing that.
Download Code Snippet ZIP File
Comments (4) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Ask Ben: Finding The SQL Data Type Of A ColdFusion Query Column
Project HUGE : Week Two Successfully Completed
Would the byte array method work with UTF-8?
Posted by Kate Maher on Jan 3, 2008 at 3:37 PM
@Kate,
Interesting question! I am not sure off-hand.
Posted by Ben Nadel on Jan 3, 2008 at 4:49 PM
Is it possible to take an XML file, read it and convert it to a single byte array? Also is it possilbe to determine the total filesize of that array?
Thanks,
Jimmy
Posted by Jimmy on Jan 29, 2008 at 7:41 PM
@Jimmy,
In the underlying Java of ColdFusion, you can take a string and convert it to a byte array by simply calling .GetTypes() on it. This returns data of type byte[].
The file size, I assume would just be the length of the array? If each index is one byte, the file size is directly mirrored by the length of the array?? I think.
Posted by Ben Nadel on Jan 30, 2008 at 8:04 AM