How to find SubString in R programming? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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() methodUsing str_detect() methodUsing grep() methodMethod 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: R # Given String gfg < - "Geeks For Geeks" # Using substr() method answer < - substr(gfg, 0, 5) print(answer) Output: [1] "Geeks" Example 2: R # Given String gfg < - "The quick brown fox jumps over the lazy dog" # Using substr() method answer < - substr(gfg, 20, 30) print(answer) 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 stringpattern: Pattern to be matched Example: R # R Program to illustrate # the use of str_detect function # Loading library library(stringr) # Creating vector x <- c("Geeks", "Hello", "Welcome", "For") # Pattern to be matched pat <- "Geeks" # Calling str_detect() function str_detect(x, pat) Output: [1] TRUE FALSE FALSE FALSEMethod 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: R str <- c("Hello", "hello", "hi", "hey") grep('he', str) Output: [1] 2 4 Comment More infoAdvertise with us Next Article How to find SubString in R programming? J Jitender_1998 Follow Improve Article Tags : R Language Similar Reads String Matching in R Programming String matching is an important aspect of any language. It is useful in finding, replacing as well as removing string(s). In order to understand string matching in R Language, we first have to understand what related functions are available in R. In order to do so, we can either use the matching str 5 min read Splitting Strings in R programming - strsplit() method strsplit() method in R Programming Language is used to split the string by using a delimiter. strsplit() Syntax: Syntax: strsplit(string, split, fixed) Parameters: string: Input vector or string.split: It is a character of string to being split.fixed: Match the split or use the regular expression. R 2 min read How to reverse a string in R Reversing a string means changing its order so that the last character becomes the first, the second last character becomes the second, and so on. A while loop is a control flow statement used in R programming to execute a block of code repeatedly as long as a specified condition is true. By using a 4 min read How to Install stringr in Anaconda stringr is the popular R package designed to make working with strings in the R easier and more intuitive. It can provide a consistent set of functions to manipulate and analyze strings. Installing the stringr in the Anaconda allows you to leverage its capabilities within the Anaconda environment wh 2 min read Find String Matches in a Vector or Matrix in R Programming - str_detect() Function 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 element of the Vector or matrix. Note: This function uses 'stringr' Library. Syntax: str_detect(string 2 min read How to find maximum string length by column in R DataFrame ? In this article, we are going to see how to find maximum string length by column in R Programming Language. To find the maximum string length by column in the given dataframe, first, nchar() function is called to get the length of all the string present in the particular column of the dataframe, an 2 min read Extracting Substrings from a Character Vector in R Programming - substring() Function substring() function in R Programming Language is used to extract substrings in a character vector. You can easily extract the required substring or character from the given string. Syntax: substring(text, first, last) Parameters: text: character vectorfirst: integer, the first element to be replac 1 min read Matching of patterns in a String in R Programming - agrep() Function agrep() function in R Language is used to search for approximate matches to pattern within each element of the given string. Syntax: agrep(pattern, x, ignore.case=FALSE, value=FALSE)Parameters:pattern: Specified pattern which is going to be matched with given elements of the string. x: Specified st 1 min read Seek a Match for the Pattern in the String in R Programming - pmatch() Function pmatch() function in R Language is used to seek match for the pattern passed as argument. It returns the strings that begin with the pattern being searched. Syntax: pmatch(pat, string, nomatch) Parameters: pat: pattern vector to be searched string: vector to be matched nomatch: return when no match 1 min read Like