Open In App

Trim a String to a Specified Display Width in R Programming – strtrim() Function

strtrim() function in R Language is used to trim a string to a specified display width.

Syntax: strtrim(x, width)



Parameters:
x: character vector
width: specified width

Example 1:






# R program to trim a string
# to specified width
  
# Creating a vector
x1 <- "GeeksforGeeks"
x2 <- "R Programming"
  
# Calling strtrim() function
strtrim(x1, 5)
strtrim(x2, 7)

Output:

[1] "Geeks"
[1] "R Progr"

Example 2:




# R program to trim a string
# to specified width
  
# Creating a vector
x <- c("Hello", "Geeks", "GFG")
  
# Calling strtrim() function
strtrim(x, 2)
strtrim(x, c(1, 2, 3))

Output:

[1] "He" "Ge" "GF"
[1] "H"   "Ge"  "GFG"
Article Tags :