How do I create an array of strings from a string, eg.
"hello world" would return ["hello", "world"]. This would need to take into account punctuation marks, etc.
There's probably a great RegEx solution for this, I'm just not capable of finding it.
How do I create an array of strings from a string, eg.
"hello world" would return ["hello", "world"]. This would need to take into account punctuation marks, etc.
There's probably a great RegEx solution for this, I'm just not capable of finding it.
Any reason that:
var myString:String = "hello world";
var reg:RegExp = /\W/i;
var stringAsArray:Array = myString.replace(reg, "").split(" ");
Won't work?
How about AS3's String.split?
var text:String = "hello world";
var split:Array = text.split(" "); // this will give you ["hello", "world"]
// then iterate and strip out any redundant punctuation like commas, colons and full stops
This seems to do what you want:
package
{
import flash.display.Sprite
public class WordSplit extends Sprite
{
public function WordSplit()
{
var inText:String = "This is a Hello World example.\nIt attempts,\
to simulate! what splitting\" words ' using: puncuation\tand\
invisible ; characters ^ & * yeah.";
var regExp:RegExp = /\w+/g;
var wordList:Array = inText.match(regExp);
trace(wordList);
}
}
}
If not, please provide a sample input and output specification.
Think I've cracked it, here is the function in full:
public static function getArrayFromString(str:String):Array {
return str.split(/\W | ' | /gi);
}
Basically, it uses the 'not a word' condition but excludes apostrophes, is global and ignores case. Thanks to everyone who pointed me in the right direction.
I think you might want something like this:
public static function getArrayFromString(str:String):Array {
return str.split(/[\W']+/gi);
}
Basically, you can add any characters that you want to be considered delimiters into the square brackets. Here's how the pieces work: