user1234440
user1234440

Reputation: 23567

Character Extraction from String

How would you extract all characters up to a specified character? For Example given, I would like to extract everything before the "." (period):

a<-c("asdasd.sss","segssddfge.sss","se.sss")

I would like to get back:

asdasd segssddfge se

I tried:

substr(a,1,".")

but it doesn't seem to work.

any ideas?

Upvotes: 6

Views: 6109

Answers (3)

agstudy
agstudy

Reputation: 121568

Here an attempt using gsub

gsub(pattern='(.*)[.](.*)','\\1', c("asdasd.sss","segssddfge.sss","se.sss"))
[1] "asdasd"     "segssddfge" "se"        

Upvotes: 2

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

Here's a very basic approach:

sapply(strsplit(a, "\\."), `[[`, 1)
# [1] "asdasd"     "segssddfge" "se"

And another:

sub(".sss", "", a, fixed = TRUE)
# [1] "asdasd"     "segssddfge" "se" 
## OR sub("(.*)\\..*", "\\1", a) 
## And possibly other variations

Upvotes: 7

Arun
Arun

Reputation: 118779

Using sub:

# match a "." (escape with "\" to search for "." as a normal "." 
# means "any character") followed by 0 to any amount of characters
# until the end of the string and replace with nothing ("")
sub("\\..*$", "", a)

Using subtr and gregexpr (assuming there's only 1 . and there's a definite match in all strings within the vector).

# get the match position of a "." for every string in "a" (returns a list)
# unlist it and get the substring of each from 1 to match.position - 1
substr(a, 1, unlist(gregexpr("\\.", a)) - 1)

Upvotes: 4

Related Questions