A string is a sequence of characters. For example, "Programming" is a string that includes characters: P, r, o, g, r, a, m, m, i, n, g.
In R, we represent strings using quotation marks (double quotes, " "
or single quotes, ' '
). For example,
# string value using single quotes
'Hello'
# string value using double quotes
"Hello"
Here, both 'Hello'
and "Hello"
are strings. We can use either single quotes or double quotes to represent strings in R.
However, we cannot mismatch them. For example, start the string with a single quote and end it with a double quote, 'Hello"
and vice versa, "Hello'
.
Example: Strings in R
message1 <- 'Hola Amigos'
print(message1)
message2 <- "Welcome to Programiz"
print(message2)
Output
[1] "Hola Amigos" [1] "Welcome to Programiz"
In the above example, we have created string variables named: message1 and message2 with values "Hola Amigos"
and "Welcome to Programiz"
respectively.
Here, when we print the string variables, we get the corresponding values. To learn more about printing values, visit R Print Output.
String Operations in R
R provides us various built-in functions that allow us to perform different operations on strings. Here, we will look at some of the commonly used string functions.
- Find the length of a string
- Join two strings
- Compare two strings
- Change the string case
1. Find Length of R String
We use the nchar()
method to find the length of a string. For example,
message1 <- "Programiz"
# use of nchar() to find length of message1
nchar(message1) # 9
Here, nchar()
returns the number of characters present inside the string.
2. Join Strings Together
In R, we can use the paste()
function to join two or more strings together. For example,
message1 <- "Programiz"
message2 <- "Pro"
# use paste() to join two strings
paste(message1, message2)
Output
[1] Programiz Pro
Here, we have used the paste()
function to join two strings: message1 and message2.
3. Compare Two Strings in R Programming
We use the ==
operator to compare two strings. If two strings are equal, the operator returns TRUE
. Otherwise, it returns FALSE
. For example,
message1 <- "Hello, World!"
message2 <- "Hola, Mundo!"
message3 <- "Hello, World!"
# compare message1 and message2
print(message1 == message2)
# compare message1 and message3
print(message1 == message3)
Output
[1] FALSE [1] TRUE
In the above example,
message1 == message2
- returnsFALSE
because two strings are not equalmessage1 == message3
- returnsTRUE
because both strings are equal
4. Change Case of R String
In R, we can change the case of a string using
toupper()
- convert string to uppercasetolower()
- convert string to lowercase
Let's see an example,
message <- "R Programming"
# change string to uppercase
message_upper <- toupper(message)
cat("Uppercase:", message_upper)
# change string to lowercase
message_lower <- tolower(message)
cat("\nLowercase:", message_lower)
Output
Uppercase: R PROGRAMMING Lowercase: r programming
Here, we have used the toupper()
and the tolower()
method to change the case of the message1 string variable to uppercase and lowercase respectively.
Note that we have used the cat() function to print the string and variable together. To learn more, visit R Print Output.
Other String Functions
Frequently Asked Questions
In R, the format()
method returns a formatted string based on the argument passed.
The syntax of the format()
method is
format(str, width, justify, ...)
Here,
str
- a string to be formattedwidth
- the minimum width to be displayedjustify
- alignment of the string i.e. left "l", right "r", or center "c"...
- various other arguments
message1 <- "Programiz"
# place string in the center
result1 <- format(message1, width = 18, justify = "c")
print(result1)
# place string in the left
result2 <- format(message1, width = 18, justify = "l")
print(result2)
# place string in the right
result3 <- format(message1, width = 18, justify = "r")
print(result3)
# Output:
# [1] " Programiz "
# [1] "Programiz "
# [1] " Programiz"
In R, we use the strsplit()
method to split strings. For example,
str1 <- "Programiz Pro"
# Using strsplit() method
result <- strsplit(str1, " ")
print(result)
# Output: "Programiz" "Pro"
In R, we use the substring()
method to update strings. For example,
info1 <- "Programes "
substring(info1, 8, 9) <- "iz"
cat("Updated String:", info1)
# Output: Updated String: Programiz
Escape Sequences in R String
The escape sequence is used to escape some of the characters present inside a string.
Suppose we need to include double quotes inside a string.
# include double quote
example1 <- "This is "R" programming"
example1 # throws error
Since strings are represented by double quotes, the compiler will treat "This is "
as the string. Hence, the above code will cause an error.
To solve this issue, we use the escape character \
in R.
# use the escape character
example1 = "This is \"R\" programming"
# use of cat() to omit backslash
cat(example1)
# Output: [1] This is "R" programming
Now the program will run without any error. Here, the escape character will tell the compiler to ignore the character after \
.
Note: Auto-printing will print the backslash in the output. To print without backlash we use the cat()
function.
Here is a list of all the escape sequences supported by R.
Escape Sequences | Character |
---|---|
\b |
backspace |
\\ |
plain backslash |
\t |
a horizontal tab |
\n |
line feed |
\" |
double quote |
Note: A double-quoted string can have single quotes without escaping them. For example,
message <- "Let's code"
print(message)
# Output : [1] "Let's Code"
R Multiline String
In R, we can also create a multiline string. However, at the end of each line break R will add "\n"
to indicate a new line. For example,
// define multiline string
message1 <- "R is awesome
It is a powerful language
R can be used in data science"
// display multiline string
print(message1)
Output
[1] "R is awesome\nIt is a powerful language\nR can be used in data science"
In the above example, we have created a multiline string and stored it in message1. Here, we have used the print()
function to print the string.
You can see we are getting \n at the end of each line break.
However, if we want to print the actual string without \n at end, we can use cat() instead of print. For example,
// define multiline string
message1 <- "R is awesome
It is a powerful language
R can be used in data science"
// use of cat() to display line breaks cat(message1)
Output
R is awesome It is a powerful language R can be used in data science