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 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 "
相關用法
- R語言 beep()用法及代碼示例
- R語言 transform()用法及代碼示例
- R語言 round()用法及代碼示例
- R語言 unlist()用法及代碼示例
- R語言 toString()用法及代碼示例
- R語言 sqrt()用法及代碼示例
- R語言 prod()用法及代碼示例
- R語言 topo.colors()用法及代碼示例
- R語言 cm.colors()用法及代碼示例
- R語言 terrain.colors()用法及代碼示例
- R語言 sweep()用法及代碼示例
注:本文由純淨天空篩選整理自akhilsharma870大神的英文原創作品 Remove Leading whitespaces from a String in R Language – trimws() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
