4

I'm having trouble comparing two strings in R. Is there a method to do it? Like in Java you have String.equals(AnotherString), is there a similar function in R?

KevThatDevs
  • 78
  • 1
  • 2
  • 7
  • 1
    You can check with `==` or `all.equal` or `identical` – akrun Feb 08 '20 at 19:55
  • In addition to the above, would encourage you to edit your question and show us what your strings look like and what you tried so far. Please see: [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Ben Feb 08 '20 at 20:07

1 Answers1

8

It's pretty simple actually.

s1 <- "string"
s2 <- "String"

# Case-sensitive check
s1 == s2

# Case-insensitive check
tolower(s1) == tolower(s2)

The output in the first case is FALSE and in the second case it is TRUE. You can use toupper() as well.

RD_
  • 146
  • 3