3 Ways to Find Character in String in R

A character is a single letter, symbol, or any printable element representing a string of 1.

A string is a sequence of characters, also known as a text, and it is represented using single quotes (”) or double quotes (“”).

Let’s see various ways to find a character or check the character’s existence in the string.

Find Character in String in R

There are 3 easy ways to find a character in a string in R.

  1. Using grepl() function
  2. Using grep() function
  3. Using gregexpr() function

Method 1: Using the grepl() function

Use the grepl() function to check if characters are present in the string. If it is present in the string, it returns TRUE; otherwise FALSE.

The grepl() function returns a logical value, suggesting whether or not the characters are present in the string.

string_first <- "TheLastOfUs"

value_first <- "L"

grepl(value_first, string_first)

value_second <- "G"

grepl(value_second, string_first)

Output

[1] TRUE
[1] FALSE

The first time, the grepl() function returns TRUE because the character “L” is present in the string “TheLastOfUs”.

The second time, the grepl() function returns FALSE because the character “G” is not present in the string “TheLastOfUs”.

Method 2: Using the grep() function

The grep() function searches for matches of a pattern within each element of the given string and returns the indices of the elements in a vector that contain a match to the given pattern.

string_first <- "TheLastOfUs"

char <- "L"

grep(char, string_first)

char_second <- "G"

grep(char_second, string_first)

Output

[1] 1
integer(0)

Method 3: Using the gregexpr() function

The gregexpr() function in R returns a substring matching the pattern passed to the function.

The gregexpr() function can find characters at different string locations.

Technique 1: Finding the location of Every Occurrence

In this example, we are finding the location of every occurrence of the character “s” using the unlist() and gregexpr() functions.

main_string <- "TheLastOfUs"

unlist(gregexpr("s", main_string))

Output

[1] 6 11

The gregexpr(“s”, main_string) function returns the list containing the indices of the elements in the string that match the regular expression “s”. In our case, the character “s” occurs on the 6th and 11th indices of the string.

Technique 2: Finding the location of the first occurrence

You can only get the first occurrence of the character in the string using the gregexpr() function by passing the [1] operator.

main_string <- "TheLastOfUs"

unlist(gregexpr("s", main_string))[1]

Output

[1] 6

The gregexpr(“s”, main_string) function returns a list containing the indices of the elements in the string that match the regular expression “s”.

Then, the [1] operator is used to extract the first element of the list, which is a vector containing the indices of the elements in the string where the letter “s” appears.

Technique 3: Finding the location of the last occurrence

The tail() function is used to extract the last element of the resulting vector.

main_string <- "TheLastOfUs"

tail(unlist(gregexpr("s", main_string)), n = 1)

Output

[1] 11

The unlist() function is used to extract the indices of the elements in the string where the letter “s” appears.

Finally, the tail() function is used to extract the last element of the resulting vector, which is the position of the last occurrence of the letter “s” in the string.

Technique 4: Finding the total number of occurrences

Use the length() function to calculate the length of the vector.

main_string <- "TheLastOfUs"

length(unlist(gregexpr("s", main_string)))

Output

[1] 2

In our example, the character “s” occurs 2 times in our main_string that’s why it returns 2.

Conclusion

Use the gregexpr() function to find a character in a string and get its index in the string.

If you want to check if a string contains a specific character in the string, use the grepl() function.

Leave a Comment