rof Brian Ripley
Sent: Thursday, September 15, 2005 10:43 AM
To: Barry Rowlingson
Cc: r-help@stat.math.ethz.ch
Subject: Re: [R] Splitting the string at the last sub-string
On Thu, 15 Sep 2005, Barry Rowlingson wrote:
> Prof Brian Ripley wrote:
>
>>> substring(str, c(1, 26), c(25,length(str
On Thu, 15 Sep 2005, Barry Rowlingson wrote:
> Prof Brian Ripley wrote:
>
>>> substring(str, c(1, 26), c(25,length(str)))
>
> nchar(str) surely?
Yes, or anything larger: I actually tested 1.
> regexps can be rather slow though. Here's two functions:
But that's not the way to do this repe
Prof Brian Ripley wrote:
>>substring(str, c(1, 26), c(25,length(str)))
nchar(str) surely?
regexps can be rather slow though. Here's two functions:
byRipley =
function(str,sub){
lp=attr(regexpr(paste(".*",sub,sep=""),str),'match.length')
return(substring(str, c(1, lp+1), c(lp,nchar(str
> regexpr(".*e", str)
[1] 1
attr(,"match.length")
[1] 25
tells you you need
> substring(str, c(1, 26), c(25,length(str)))
[1] "Chance favors the prepare" "d mind"
to reproduce your answer (I don't know what you want to do with the
substring, but you included it in the first string, which is not
Hi,
I need to split a string into 2 strings, with the split point defined by the
last occurrence of some substring. I come up with some convoluted code to do
so:
str = "Chance favors the prepared mind"
sub = "e"
y = unlist(strsplit(str,sub))
z = cbind(paste(y[-length(y)], sub, sep="", collapse