Open In App

R Strings

Last Updated : 07 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Strings are a bunch of character variables. It is a one-dimensional array of characters. One or more characters enclosed in a pair of matching single or double quotes can be considered a string in R.

It represents textual content and can contain numbers, spaces, and special characters. An empty string is represented by using "". R Strings are always stored as double-quoted values.

Note: A double-quoted string can contain single quotes within it. Single-quoted strings can't contain single quotes. Similarly, double quotes can't be surrounded by double quotes.

1. Creation of String in R

R Strings can be created by assigning character values to a variable. These strings can be further concatenated by using various functions and methods to form a big string. 

R
str1 <- "OK1"
cat ("String 1 is : ", str1)

str2 <- 'OK2'
cat ("String 2 is : ", str2)

str3 <- "This is 'acceptable and 'allowed' in R"
cat ("String 3 is : ", str3)

str4 <- 'Hi, Wondering "if this "works"'
cat ("String 4 is : ", str4)

str5 <- 'hi, ' this is not allowed'
cat ("String 5 is : ", str5)

Output

String 1 is: OK1
String 2 is: OK2
String 3 is: This is 'acceptable and 'allowed' in R
String 4 is: Hi, Wondering "if this "works"
Error: unexpected symbol in " str5 <- 'hi, ' this"
Execution halted

2. Length of String

The length of strings indicates the number of characters present in the string.

2.1 Using the str_length() function

The function str_length() belonging to the 'string' package. It can be used to determine the length of strings.

R
library(stringr)
 
str_length("hello")

Output

5

2.2 Using nchar() function 

nchar() is a inbuilt function of R and can be used to determine the length of strings in R. 

R
nchar("hel'lo")

Output

6

3. Accessing portions of an R string

The individual characters of a string can be extracted from a string by using the indexing methods of a string. There are two R's inbuilt functions in order to access both the single character as well as the substrings of the string. 

substr() or substring() function in R extracts substrings out of a string beginning with the start index and ending with the end index. It also replaces the specified substring with a new set of characters. 

Syntax

substr(..., start, end)
#OR
substring(..., start, end)

3.1. Using substr() function 

If the starting index is equal to the ending index, the corresponding character of the string is accessed.

R
substr("Learn Code Tech", 1, 1)

Output

"L" 

3.2. Using substring() function 

Here, the number of characters in the string is 10. The first print statement prints the last character of the string, "e", which is str[10]. The second print statement prints the 11th character of the string, which doesn't exist, but the code doesn't throw an error and print "", that is an empty character.

R
str <- "Learn Code"

len <- nchar(str)

print (substring(str, len, len))

print (substring(str, len+1, len+1))

Output

[1] "e" 

The following R code indicates the mechanism of String Slicing, where in the substrings of a R string are extracted: 

R
str <- "Learn Code"

len <- nchar(str)
print(substr(str, 1, 4))
print(substr(str, len-2, len))

Output

[1]"Lear"
[1]"ode"

The first print statement prints the first four characters of the string. The second print statement prints the substring from the indexes 8 to 10, which is "ode".

4. Case Conversion

The R string characters can be converted to upper or lower case by R's inbuilt function

  • toupper() converts all the characters to upper case
  • tolower() converts all the characters to lower case
  • casefold(..., upper=TRUE/FALSE) converts on the basis of the value specified to the upper argument.

All these functions can take in as arguments multiple strings too. The time complexity of all the operations is O(number of characters in the string). 

R
str <- "Hi LeArn CodiNG"
print(toupper(str))
print(tolower(str))
print(casefold(str, upper = TRUE))

Output

[1] "HI LEARN CODING"
[1] "hi learn coding"
[1] "HI LEARN CODING"

By default, the value of upper in casefold() function is set to FALSE. If we set it to TRUE, the R string gets printed in upper case.

5. Concatenation of R Strings

We can concatenate strings using paste() function.

In this example, we first create two strings "Hello" and "World" and store them in the variables string1 and string2, respectively. We then use the paste() function to concatenate the two strings together with a space between them and store the result in the variable result. Finally, we use the print function to print the value of result to the console. 

R
string1 <- "Hello"
string2 <- "World"

result <- paste(string1, string2)

print(result)

Output

"Hello World"

We can see the output is Hello World, which is the concatenated string of "Hello" and "World" with a space between them.

5.1. Concatenation of Multiple Strings

We can also concatenate multiple strings by passing them as separate arguments to the paste function.

In this example, we concatenate three strings "Hello", "to", and "the World" and store the result in the variable result. The paste function combines the strings together with a space between them, so the output would be Hello to the World.

R
result <- paste("Hello", "to", "the World")

print(result)

Output

[1] "Hello to the World"

6. String formatting

String formatting in R is performed using the sprintf function.

In this example, we format a string with two decimal places using the %d format specifier for the integer value x and the %.2f format specifier for the floating-point value y. The prepared string is saved in the variable result before being written to the console using the print function. The solution is 42, and pi is 3.14, which is the formatted string with x and y values substituted for the format specifiers.

R
x <- 42
y <- 3.14159

result <- sprintf("The answer is %d, and pi is %.2f.", x, y)

print(result)

Output

[1] "John is 35 years old and 1.80 meters tall."

7. Updating the Strings

The characters, as well as substrings of a string, can be manipulated to new string values. The changes are reflected in the original string.

Syntax:

substr (..., start, end) <- newstring
substring (..., start, end) <- newstring

Multiple strings can be updated at once, with the start <= end. But:

  • If the length of the substring is larger than the new string, only the portion of the substring equal to the length of the new string is replaced.
  • If the length of the substring is smaller than the new string, the position of the substring is replaced with the corresponding new string values.
R
string <- "Hello, World!"

string <- gsub("World", "Universe", string)

print(string)

Output

"Hello, Universe!"


Next Article

Similar Reads