create an array of words (Strings) from String
Solution 1
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.
Solution 2
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
Solution 3
Here's what you need. Tested and working:
private function splitString(str:String):Array {
var r:RegExp = /\W+/g;
return str.split(r));
}
http://snipplr.com/view/63811/split-string-into-array/
Solution 4
Any reason that:
var myString:String = "hello world";
var reg:RegExp = /\W/i;
var stringAsArray:Array = myString.replace(reg, "").split(" ");
Won't work?
Solution 5
Maybe this one works too...
public static function getArrayFromString(str:String):Array {
return str.split(/[^,\.\s\n\r\f¿\?¡!]+/gi);
}
That should work in languages other than English, for example (i.e. '\w' won't accept accented characters, for instance...)
Related videos on Youtube
hamishtaplin
Updated on June 04, 2022Comments
-
hamishtaplin almost 2 years
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.
-
hamishtaplin almost 14 yearsThat's completely unhelpful, thanks.
-
-
hamishtaplin almost 14 yearsIt's the stripping out the punctuation I'm interested in. I know how to do this with a rather clunky if/else - I'm looking for a more elegant solution though (enter RegExp..)
-
James Fassett almost 14 yearsAs I said you need to provide a complete input and output specification. I can't keep guessing what you believe does and doesn't constitute a word.
-
hamishtaplin almost 14 yearsIt's pretty obvious what does and doesn't constitute a word, no guessing required. Thanks for the help though.
-
James Fassett almost 14 yearsNot as obvious as you think. You need an apostrophe now. How about hyphenated words? Do you consider currency ($100) a word? Your regular expression will become your specification.
-
Danyal Aytekin almost 14 yearsIt's good you have something you are happy with. Like a newborn child, take a photo of this, because I think it is the last time you will see it so small. Other unwanted characters are on their way, such as the other species of apostrophe. Feel free to post the regex back here for interest's sake if it becomes particularly frightening...
-
hamishtaplin almost 14 yearsYou are right of course, thanks for pointing that out. For now, I don't think that'll be a problem though, hopefully.
-
hamishtaplin almost 14 yearsThanks, I will try that out, too.