Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all characters before a period in a string

Tags:

string

regex

r

This keeps everything before a period:

gsub("\\..*","", data$column )

how to keep everything after the period?

like image 899
user1320502 Avatar asked Sep 23 '14 09:09

user1320502


People also ask

How do you remove everything before a certain character in a string?

To remove everything before the first occurrence of the character '-' in a string, pass the character '-' as a separator in the partition() function. Then assign the part after the separator to the original string variable. It will give an effect that we have deleted everything before the character '-' in a string.

How do you delete everything before a character in a string Python?

If you need to remove everything before last occurrence of a character, use the str. rfind() method.

How do you delete everything before a character in a string Java?

. trim() removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.) of a string (leading spaces) and also removes spaces after the last character (trailing spaces).

How do I remove all characters from a string after a specific character?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.


1 Answers

To remove all the characters before a period in a string(including period).

gsub("^.*\\.","", data$column )

Example:

> data <- 'foobar.barfoo'
> gsub("^.*\\.","", data)
[1] "barfoo"

To remove all the characters before the first period(including period).

> data <- 'foo.bar.barfoo'
> gsub("^.*?\\.","", data)
[1] "bar.barfoo"
like image 117
Avinash Raj Avatar answered Sep 28 '22 10:09

Avinash Raj