VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-find-substring-in-r-programming/

⇱ How to find SubString in R programming? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to find SubString in R programming?

Last Updated : 12 Jul, 2025

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:

  • Using substr() method
  • Using str_detect() method
  • Using grep() method

Method 1: Using substr() method

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"

Method 2: Using str_detect() method

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

Method 3: Using grep() method

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
Comment
Article Tags:
Article Tags:

Explore