Repeated string without a loop
A problem comes up
This week I had a relatively simple problem that I needed to solve. Because it was simple and I didn't like the obvious solution, I thought I'd share the solution I came up with.
The problem was this:
- I had a string where each character represented some data as part of a collection of data.
- I had to change these characters out of order (for example: character 196, then 13, then 87, etc)
My approach - Create a string of n-length
The approach I decided on was to start by creating a string of the final length I would need then update each character as I got the information I needed. Should be simple enough to create a string of a certain length but alas, not so simple as to have a String.createStringOfLength() method.
The obvious method
My first method of doing this is the obvious loop method. I'm a big fan of loops so it looked like this:
function createStringOfLength(length:uint):String {
// we'll use 'x' as our default character - spaces are fine too.
var output:String = "";
for(var index:uint = 0; index < length; index++){
output += "x";
}
return output;
}
And that function works fine, it is understandable but it just seems that there should be a more elegant solution.
A more elegant solution
The problem with the method above is that I can't just create a string of an arbitrary length but I feel like I should be able to. Is there something else in AS3 that you can create of an arbitrary length? Yes! You can create a Vector of a specific length and use default values to boot.
Here's where it starts to get elegant. Since Strings really are just an array of characters this correlation makes a lot of sense. The code above can actually be recreated in 2 lines (not counting the function definition and enclosing curly braces).
function createStringOfLength(length:uint):String {
var output:Vector.<uint> = new Vector.<uint>(count, true);
return output.join("").replace(/0/g, "x");
}
I used a few tricks of the language here, so some of this may not be obvious. First, in AS3, the default value for a uint
is 0
. That is the reason I search for it in the pattern for the replace call. Second, and really the main thing, I took advantage of the ability to create a Vector of a specified length and fill it with its type
(in this case uint
) defaults. Finally, I just used the Vector.join
method and replaced the default 0
with whatever character I wanted. Admittedly, I could have just left them all as 0
but the replace step was so simple I thought I'd throw it in.
Conclusion
There probably wasn't a performance reason for me to create this solution, and I honestly don't know if it is any more performant. Looking at new ways to relate to a problem is the bread and butter of a programmer though. So, if you've ever wanted to create a string of a specified length or repeat any string a certain number of times here is a new way to think about the problem.
Do you have another solution? If so, I'd love to hear it. Did you like my solution? Hate it? Have an improvement? Let me know in the comments below or share a link to your code. Github's gists are a great way to share code snippets - as I just did right there.