Remove Leading whitespaces from a String in R Language – trimws() Function

trimws() function in R Language is used to trim the leading white spaces. It shrinks an object by removing outermost rows and columns with the same values.

Syntax: trimws(x)

Parameters:
x: Object or character string

Example 1: Remove Leading and Trailing Whitespace

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))
  

chevron_right


Output:



".  This is a trim       function      example"

Here in the above code, we have created a character string and have white spaces in it, So we have used the trimws() function to remove the white spaces.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

"Geeks_For_Geeks     "
My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.