从 R 语言中的字符串中删除前导空格 - trimws()函数
在本文中,我们将了解如何在 R 编程语言中从字符串中删除空格。
R – trimws()函数
R 语言中的trimws()函数用于修剪前导空格。它通过删除具有相同值的最外层行和列来缩小对象。
Syntax: trimws(x)
Parameters:
- x: Object or character string
示例 1:删除前导和尾随空格
R
# R program to illustrate
# Removing of leading whitespaces
# Create example character string
my_text <- " . This is a trim function example "
# Apply trimws function in R
print(trimws(my_text))
R
# R program to illustrate
# Removing of leading whitespaces
# Create example character string
my_text <- " Geeks_For_Geeks "
# Apply trimws function in R
print(trimws(my_text))
输出:
". This is a trim function example"
在上面的代码中,我们创建了一个字符并在其中包含字符串,因此我们使用了 trimws()函数来删除空格。
示例 2:从 R 中的字符串中删除空格
R
# R program to illustrate
# Removing of leading whitespaces
# Create example character string
my_text <- " Geeks_For_Geeks "
# Apply trimws function in R
print(trimws(my_text))
输出:
"Geeks_For_Geeks "