R Strings
Single quotes or double quotes are used to represent Strings in R programming language. We shall learn rules to write a String, embed special characters in it, and some of the common operations like concatenation of two strings, finding length of string, etc.
Rules to write R Strings
Following are the rules to write a valid string in R programming language :
- Either single quotes or double quotes should be used to enclose a string value.
str1 =‘Hello‘ str2 =“Hello“
- As only single quote or double quotes are the special characters used to represent an R string, to include them in a String requires special care.
- To include a single quote inside a string value, surround the single quote with double quotes.
For Who’s hello is that? str1 = “Who”‘“s hello is that?”
- To include a double quote inside a string value, surround the double quote with single quotes.
ForR says, “Hello”. str1 = “R says, ‘“‘Hello’“‘.”
- Single quote or double quote could be included at the starting or ending of the string value as is.
For‘Hello.” str = “‘Hello.““
How to find length of String in R
To find the length of a String in R, use nchar() function. Following is the syntax of nchar function.
nchar(x)
where x is of character data type
String Length in R
Example to find length of String in R
In this example, we will initialize a variable with a String value and find its length using nchar(string) function.
# Example R program to find length of string str = 'Hello World!' # find length using nchar(c) length = nchar(str) print (length)
Output
$ Rscript r_strings_length.R
[1] 12
Example to find length of a number in R
nchar automatically type casts number to character
Find length of String # Example R program to find length of number num = 523641 # find length using nchar(c) length = nchar(num) print (length) Output Rscript r_strings_length.R [1] 6
You may try nchar(c) function for other data types to find the length.
Extract Substring from a String in R
Syntax of R substring function
substring(c, first, last)
where :
c is the stringfirst is the starting position of substring (in the main string) to be extractedlast is the ending position of substring (in the main string) to be extracted. last is optional, in which case last is taken as whole length of the string.
Example to extract Substring from a String in R
extract Substring from a String # Example R program to find substring str = 'Hello World! Welcome to learning R programming language' substring(c, first, last) subStr = substring(str, 13, 23) print (subStr) Output $ Rscript r_strings_substring.R [1] " Welcome to"
Example to extract Substring from a String in R without last position provided
extract Substring from a String # Example R program to find substring str = 'Hello World! Welcome to learning R programming language' substring(c, first, last=nchar(c)) subStr = substring(str, 13) print (subStr) Output $ Rscript r_strings_substring.R [1] " Welcome to learning R programming language"
Concatenate Strings
To concatenate strings in r programming, use paste() function. The syntax of paste function that is used to concatenate two or more strings.
paste(…, sep="", collapse=NULL)
where
| paste | is the keyword |
| … | input strings separated by comma |
| sep | is a character that would be appended between two adjacent strings and acts as a separator |
| collapse | is an optional character to separate the results |
Concatenate two or more Strings in R
While concatenating strings in R, we can choose the separator and number number of input strings. Following examples demonstrate different scenarios while concatenating strings in R using paste() function.
Example: Concatenate Strings in R
In this example, we will use paste() function with default separator.
Concatenate two strings #Example R program to concatenate two strings str1 = 'Hello' str2 = 'World!' # concatenate two strings using paste function result = paste(str1,str2) print (result) Output $ Rscript r_strings_concatenate_two.R [1] "Hello World!"
The default separator in paste function is space ” “. So ‘Hello’ and ‘World!’ are joined with space in between them.
Example: Concatenate Strings in R with no separator
In this example, we shall use paste() function with the separator as emtpy, i.e., sep=””.
Concatenate two strings with custom separator #Example R program to concatenate two strings str1 = 'Hello' str2 = 'World!' #concatenate two strings using paste function result = paste(str1,str2,sep="") print (result) Output $ Rscript r_strings_concatenate_two.R [1] "HelloWorld!"
Please observe that we have overwritten default separator in paste function with “”. So ‘Hello’ and ‘World!’ are joined with nothing in between them.
Example: Concatenate Multiple Strings
In this example, we shall use paste() function with the separator as hyphen, i.e., sep=”-” and also we take multiple strings at once.
concatenate more than two strings # Example R program to concatenate two strings str1 = 'Hello' str2 = 'World!' str3 = 'Join' str4 = 'Me!' # concatenate two strings using paste function result = paste(str1,str2,str3,str4,sep="-") print (result) Output $ Rscript r_strings_concatenate_multiple.R [1] "Hello-World!-Join-Me!" You may provide as many strings as required followed by the separator to the R paste() function to concatenate.
