5

I would like to check if a string contain only one type of character

For example

INPUT: 

str = "AAAAB"
char = "A"

OUTPUT:

str contains only char = FALSE

With grepl(char,str) the result is TRUE but I want it to be FALSE.

Many thanks

user3910073
  • 511
  • 1
  • 6
  • 23
  • 2
    You may need some special logic if your `str` is `""`. For instance, [m0nhawk's solution](http://stackoverflow.com/a/25159415/452096) will output `TRUE` in this case (`!grepl("[^A]", "")`), which may or may not be what you want. – Stephan Kolassa Aug 06 '14 at 11:45

5 Answers5

5

If you want to check for a specific character (in char):

str <- "AAAAB"
char = "A"
all(unlist(strsplit(str, "")) == char)
#[1] FALSE

str <- "AAAA"
char = "A"
all(unlist(strsplit(str, "")) == char)
#[1] TRUE

Or, if you want to check if the string contains only one unique character (any one):

str <- "AAAAB"
length(unique(unlist(strsplit(str, "")))) == 1
#[1] FALSE

str = "AAAA"
length(unique(unlist(strsplit(str, "")))) == 1
#[1] TRUE
talat
  • 68,970
  • 21
  • 126
  • 157
4

You need to use not operator for regex, in your case this would be:

> !grepl("[^A]", "AAA")
[1] TRUE
> !grepl("[^A]", "AAAB")
[1] FALSE

With variables:

grepl(paste0("[^", char, "]"), srt)
m0nhawk
  • 22,980
  • 9
  • 45
  • 73
4

Another option is to use charToRaw and check if it is unique:

xx <- "AAAAB"
length(unique(charToRaw(xx))) ==1
[1] FALSE
agstudy
  • 119,832
  • 17
  • 199
  • 261
3

Using gregexpr

char = "A"

str = "AAAA"
length(unlist(gregexpr(char, str))) == nchar(str)
## [1] TRUE

str = "ABAAA"
length(unlist(gregexpr(char, str))) == nchar(str)
## [1] FALSE
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
3

You can use the stri_count from the "stringi" package, like this:

library(stringi)
stri_count_fixed(str, char) == nchar(str)
# [1] FALSE
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485