R 字符串

字符串是字符序列。例如,"Programming" 是一个字符串,包含字符:P, r, o, g, r, a, m, m, i, n, g

在 R 中,我们使用引号(双引号 " " 或单引号 ' ')来表示字符串。例如,

# string value using single quotes
'Hello' 

# string value using double quotes 
"Hello" 

这里,'Hello'"Hello" 都是字符串。我们可以使用单引号或双引号来表示 R 中的字符串。

但是,我们不能混用它们。例如,用单引号开始字符串,用双引号结束字符串,如 'Hello",反之亦然,如 "Hello'


示例:R 中的字符串

message1 <- 'Hola Amigos'
print(message1)

message2 <- "Welcome to Programiz"
print(message2)

输出

[1] "Hola Amigos"
[1] "Welcome to Programiz"

在上面的示例中,我们创建了名为 message1message2 的字符串变量,其值分别为 "Hola Amigos""Welcome to Programiz"

这里,当我们打印字符串变量时,会得到相应的值。要了解更多关于打印值的信息,请访问 R 打印输出


R 中的字符串操作

R 为我们提供了各种内置函数,允许我们对字符串执行不同的操作。这里,我们将介绍一些常用的字符串函数。

  • 查找字符串的长度
  • 连接两个字符串
  • 比较两个字符串
  • 更改字符串大小写

1. 查找 R 字符串的长度

我们使用 nchar() 方法来查找字符串的长度。例如,

message1 <- "Programiz"

# use of nchar() to find length of message1 
nchar(message1) # 9

这里,nchar() 返回字符串中存在的字符数。


2. 连接字符串

在 R 中,我们可以使用 paste() 函数将两个或多个字符串连接起来。例如,

message1 <- "Programiz"
message2 <- "Pro"

# use paste() to join two strings
paste(message1, message2)

输出

[1] Programiz Pro

这里,我们使用了 paste() 函数来连接两个字符串:message1message2


3. 在 R 编程中比较两个字符串

我们使用 == 运算符来比较两个字符串。如果两个字符串相等,运算符返回 TRUE。否则,它返回 FALSE。例如,

message1 <- "Hello, World!"
message2 <- "Hola, Mundo!"
message3 <- "Hello, World!"

# compare message1 and message2 
print(message1 == message2)

# compare message1 and message3 
print(message1 == message3)

输出

[1] FALSE
[1] TRUE

在上面的例子中:

  • message1 == message2 - 返回 FALSE,因为两个字符串不相等
  • message1 == message3 - 返回 TRUE,因为两个字符串相等

4. 更改 R 字符串的大小写

在 R 中,我们可以使用以下方法更改字符串的大小写:

  • toupper() - 将字符串转换为大写
  • tolower() - 将字符串转换为小写

让我们看一个例子,

message <- "R Programming"

# change string to uppercase
message_upper <- toupper(message)
cat("Uppercase:", message_upper)

# change string to lowercase
message_lower <- tolower(message)
cat("\nLowercase:", message_lower)

输出

Uppercase: R PROGRAMMING
Lowercase: r programming

这里,我们使用了 toupper()tolower() 方法分别将 message1 字符串变量的大小写更改为大写和小写。

请注意,我们使用了 cat() 函数来同时打印字符串和变量。要了解更多信息,请访问 R 打印输出


其他字符串函数

常见问题

如何以指定样式格式化字符串?

在 R 中,format() 方法根据传递的参数返回格式化后的字符串。

format() 方法的语法是

format(str, width, justify, ...)

这里,

  • str - 要格式化的字符串
  • width - 要显示的最小宽度
  • justify - 字符串的对齐方式,即左对齐 "l"、右对齐 "r" 或居中 "c"
  • ... - 其他各种参数
message1 <- "Programiz"

# place string in the center
result1 <- format(message1, width = 18,  justify = "c")
print(result1)

# place string in the left
result2 <- format(message1, width = 18,  justify = "l")
print(result2)

# place string in the right
result3 <- format(message1, width = 18,  justify = "r")
print(result3)

# Output:
# [1] "    Programiz     "
# [1] "Programiz         "
# [1] "         Programiz"
如何在 R 中拆分字符串?

在 R 中,我们使用 strsplit() 方法来拆分字符串。例如,

str1 <- "Programiz Pro"
 
# Using strsplit() method
result <- strsplit(str1, " ")
 
print(result)

# Output: "Programiz" "Pro"
如何在 R 中更新字符串?

在 R 中,我们使用 substring() 方法来更新字符串。例如,

info1 <- "Programes "
substring(info1, 8, 9) <- "iz"

cat("Updated String:", info1)

# Output: Updated String: Programiz

R 字符串中的转义序列

转义序列用于转义字符串中存在的某些字符。

假设我们需要在字符串中包含双引号。

# include double quote
example1 <- "This is "R" programming"

example1 # throws error

由于字符串由双引号表示,编译器会将 "This is " 视为字符串。因此,上述代码将导致错误。

为了解决这个问题,我们在 R 中使用转义字符 \

# use the escape character
example1 = "This is \"R\" programming"

# use of cat() to omit backslash
cat(example1)

# Output: [1] This is "R" programming

现在程序将没有任何错误地运行。这里,转义字符将告诉编译器忽略 \ 后面的字符。

注意:自动打印将在输出中打印反斜杠。要打印不带反斜杠的字符串,我们使用 cat() 函数。

这是 R 支持的所有转义序列的列表。

转义序列 字符
\b 退格
\\ 纯反斜杠
\t 水平制表符
\n 换行
\" 双引号

注意:双引号字符串可以包含单引号而无需转义。例如,

message <- "Let's code"

print(message) 

# Output : [1] "Let's Code"

R 多行字符串

在 R 中,我们还可以创建多行字符串。但是,在每个换行符的末尾,R 将添加 "\n" 以表示新行。例如,

// define multiline string
message1 <- "R is awesome
It is a powerful language
R can be used in data science"

// display multiline string
print(message1)

输出

[1] "R is awesome\nIt is a powerful language\nR can be used in data science"

在上面的示例中,我们创建了一个多行字符串并将其存储在 message1 中。这里,我们使用了 print() 函数来打印字符串。

您可以看到我们在每个换行符的末尾都得到了 \n。

但是,如果我们想打印没有 \n 的实际字符串,我们可以使用 cat() 而不是 print。例如,

// define multiline string
message1 <- "R is awesome
It is a powerful language
R can be used in data science"

// 使用 cat() 显示换行符 cat(message1)

输出

R is awesome
It is a powerful language
R can be used in data science
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战