R语言 去除字符串中的前导空白 – trimws()函数
在这篇文章中,我们将看到如何 在R语言中删除字符串中的空白 。
R – trimws()函数
R语言 中的trimws()函数 是用来修剪前面的空白。它通过删除最外层的相同数值的行和列来缩小一个对象。
语法: trimws(x)
参数
- x: 对象或字符串
例子1:删除前面和后面的空白
# 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))
输出
". This is a trim function example"
在上面的代码中,我们创建了一个字符串,其中有空格,所以我们使用了trimws()函数来删除空格。
例2:在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 "