如何在 R 中检查字符串是否为小写或大写?


我们可以使用 str_detect 函数来检查单个字符串或字符串向量是否为小写或大写。 除了 str_detect 函数之外,我们还需要使用 upper 或 lower 来检查字符串是否为小写或大写,并且输出将以 TRUE 或 FALSE 的形式返回,如果字符串为小写,并且我们将 lower 与 str_detect 函数一起使用,则输出将为 TRUE,反之亦然。

示例

 在线演示

x1<-letters[1:26]
x1

输出

[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"

示例

str_detect(x1,"[[:upper:]]")

输出

[1]  FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE

示例

 在线演示

x2<-LETTERS[1:26]
x2

输出

[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"

示例

str_detect(x2,"[[:upper:]]")

输出

[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

示例

 在线演示

x3<-"india"
x3

输出

[1] "india"

示例

str_detect(x3,"[[:upper:]]")

输出

[1] FALSE

示例

 在线演示

x4<-c("abc","abcd","abcde","bacdef")
x4

输出

[1] "abc" "abcd" "abcde" "bacdef"

示例

str_detect(x4,"[[:upper:]]")

输出

[1] FALSE FALSE FALSE FALSE

示例

 在线演示

x5<-c("AK", "AL", "AR", "AS", "AZ", "CA", "CO", "CT", "DC", "DE", "FL", "GA", "GU", "HI", "IA", "ID", "IL", "IN", "KS", "KY", "LA", "MA", "MD", "ME", "MI", "MN", "MO", "MP", "MS", "MT", "NC", "ND", "NE", "NH", "NJ", "NM", "NV", "NY", "OH", "OK", "OR", "PA", "PR", "RI", "SC", "SD", "TN", "TX", "UM", "UT", "VA", "VI", "VT", "WA", "WI", "WV", "WY")
x5

输出

[1] "AK" "AL" "AR" "AS" "AZ" "CA" "CO" "CT" "DC" "DE" "FL" "GA" "GU" "HI" "IA"
[16] "ID" "IL" "IN" "KS" "KY" "LA" "MA" "MD" "ME" "MI" "MN" "MO" "MP" "MS" "MT"
[31] "NC" "ND" "NE" "NH" "NJ" "NM" "NV" "NY" "OH" "OK" "OR" "PA" "PR" "RI" "SC"
[46] "SD" "TN" "TX" "UM" "UT" "VA" "VI" "VT" "WA" "WI" "WV" "WY"

示例

str_detect(x5,"[[:upper:]]")

输出

[1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[16] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[31] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[46] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

示例

 在线演示

x6<-c("alabama", "alaska", "american samoa", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "district of columbia", "florida", "georgia", "guam", "hawaii", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts", "michigan", "minnesota", "minor outlying islands", "mississippi", "missouri", "montana", "nebraska", "nevada", "new hampshire", "new jersey", "new mexico", "new york", "north carolina", "north dakota", "northern mariana islands", "ohio", "oklahoma", "oregon", "pennsylvania", "puerto rico", "rhode island", "south carolina", "south dakota", "tennessee", "texas", "u.s. virgin islands", "utah", "vermont", "virginia", "washington", "west virginia", "wisconsin", "wyoming")
x6

输出

[1] "alabama" "alaska"
[3] "american samoa" "arizona"
[5] "arkansas" "california"
[7] "colorado" "connecticut"
[9] "delaware" "district of columbia"
[11] "florida" "georgia"
[13] "guam" "hawaii"
[15] "idaho" "illinois"
[17] "indiana" "iowa"
[19] "kansas" "kentucky"
[21] "louisiana" "maine"
[23] "maryland" "massachusetts"
[25] "michigan" "minnesota"
[27] "minor outlying islands" "mississippi"
[29] "missouri" "montana"
[31] "nebraska" "nevada"
[33] "new hampshire" "new jersey"
[35] "new mexico" "new york"
[37] "north carolina" "north dakota"
[39] "northern mariana islands" "ohio"
[41] "oklahoma" "oregon"
[43] "pennsylvania" "puerto rico"
[45] "rhode island" "south carolina"
[47] "south dakota" "tennessee"
[49] "texas" "u.s. virgin islands"
[51] "utah" "vermont"
[53] "virginia" "washington"
[55] "west virginia" "wisconsin"
[57] "wyoming"

示例

str_detect(x6,"[[:upper:]]")

输出

[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
[49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

更新于: 2020-10-16

3K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告