10 Feb R String Operations
In this lesson, we will work around the following useful string operations in the R programming language:
- Length of String
- Concatenate Strings
- Convert a string to uppercase
- Convert a string to lowercase
Length of String
The nchar() method is used in the R programming language to get the length of a string. It gets the number of characters in the string. Let us see an example:
1 2 3 4 5 6 7 8 9 10 |
# Our string myStr <- "Amit Diwan" # Display the string myStr # Get the length of the string nchar(myStr) |
Output
1 2 3 4 |
[1] "Amit Diwan" [1] 10 |
Concatenate Strings
The paste() method is used in the R programming language to concatenate strings. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 |
# Our strings myStr1 <- "Virat" myStr2 <- "Kohli" # Display the strings myStr1 myStr2 # Concatenate the strings paste(myStr1, myStr2) |
Output
1 2 3 4 5 |
[1] "Virat" [1] "Kohli" [1] "Virat Kohli" |
Convert to Uppercase
To convert a string to uppercase, use the toupper() method in the R programming language:
1 2 3 4 5 6 7 8 9 10 |
# Our string myStr <- "john" # Display the string myStr # Convert to uppercase toupper(myStr) |
Output
1 2 3 4 |
[1] "john" [1] "JOHN" |
Convert to Lowercase
To convert a string to lowercase, use the tolower() method in the R programming language:
1 2 3 4 5 6 7 8 9 10 |
# Our string myStr <- "AMIT" # Display the string myStr # Convert to lowercase tolower(myStr) |
Output
1 2 3 4 |
[1] "AMIT" [1] "amit" |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments