Open In App

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

Last Updated : 12 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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"

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads