开发者

Keep the last 9 digits of an alphanumeric string in R

开发者 https://www.devze.com 2023-03-31 16:26 出处:网络
Please R-gurus, how can I keep the last 9 digits of an alphanumeric string for e.g. LA XAN 00026299开发者_StackOverflow9444

Please R-gurus, how can I keep the last 9 digits of an alphanumeric string for e.g.

LA XAN 00026299开发者_StackOverflow9444

RA XAN 000263000507

WA XAN 000263268038

SA XAN 000263000464

000263000463

000263000476

I only want to get

262999444

263000507

263268038

263000464

263000463

263000476

Thanks a lot


It's pretty easy in stringr because sub_str interprets negative indices as offsets from the end of the string.

library(stringr)
str_sub(xx, -9, -1)


If you just want the last 9 positions, you could just use substr:

substr(xx,nchar(xx) - 8,nchar(xx))

assuming that your character vector is stored in xx. Also, as Hadley notes below, nchar will return unexpected things if xx is a factor, not a character vector. His solution using stringr is definitely preferable.


Assuming the input is a vector named "strgs":

sub(".*(.........)$", "\\1", strgs)
#[1] "262999444" "263000507" "263268038" "263000464"

?sub
?regex


Not really sure which language you are looking for but here would be a c# implementation.

The logic would be something like :

    string s = "WA XAN 000263268038";
    s = s.Substring(s.Length - 10, 9);

Hope this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消