DEV Community

tejaswini-59
tejaswini-59

Posted on

R - Strings

Strings are basically 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 as a string in R. Strings represent textual content and can contain numbers, spaces and special characters. An empty string is represented by using “”. Strings are always stored as double-quoted values in R. 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.

Creation of String
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.
Example:

R program for String Creation

# creating a string with double quotes
str1 <- "OK1"
cat ("String 1 is : ", str1)

creating a string with single quotes

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

Top comments (0)