problem-12.10
problem-12.10
E.10 (p. 408)
This is easier to do with regular expressions, but if you don't know
these you can split by " " and then throw out words of
length 0. This can be done using the String class methods. For example:
String$defineMethod("strip",function() {
lengthx = function(x) {
x = String$new(x)
if(x$length() > 0)
TRUE
else
FALSE
}
y = split(" ")
aword = sapply(y, lengthx)
words = y[aword]
## join with a space
paste(words,collapse=" ")
})
This would have been easier if the output of split() were a
vector of type String, but this isn't possible. Instead, the
function sapply() is used to apply the function
lengthx(), which calculates the length of the string to each
element in the output of split().