![]() |
VOOZH | about |
In this article, we are going to see how to find substring in R programming language.
R provides different ways to find substrings. These are:
Find substring in R using substr() method in R Programming is used to find the sub-string from starting index to the ending index values in a string.
Syntax: substr(string_name, start, end)
Return: Returns the sub string from a given string using indexes.
Example 1:
Output:
[1] "Geeks"
Example 2:
Output:
[1] " jumps over"
str_detect() Function in R Language is used to check if the specified match of the substring exists in the original string. It will return TRUE for a match found otherwise FALSE against each of the elements of the Vector or matrix.
Syntax: str_detect(string, pattern)
Parameter:
- string: specified string
- pattern: Pattern to be matched
Example:
Output:
[1] TRUE FALSE FALSE FALSE
grep() function returns the index at which the pattern is found in the vector. If there are multiple occurrences of the pattern, it returns a list of indices of the occurrences. This is very useful as it not only tells us about the occurrence of the pattern but also of its location in the vector.
Syntax: grep(pattern, string, ignore.case=FALSE)
Parameters:
- pattern: A regular expressions pattern.
- string: The character vector to be searched.
- ignore.case: Whether to ignore case in the search. Here ignore.case is an optional parameter as is set to FALSE by default.
Example:
Output:
[1] 2 4