str_c() Function of stringr Package in R

In this article, we will discuss str_c() function which is available in stringr package. Before going to this method, we have to install and import stringr package.

Syntax:

Install- install.packages("stringr")
Import - library("stringr")

str_c() Methods

This function is used to combine the multiple strings by a separator.

Syntax:

str_c(“string1″,”string2”,……………,sep)

where,

strings are the input strings separated by comma and sep is the second parameter used to separate deach string with a delimiter.

Example 1:

In this example, we are concatenating 5 strings without separator.

R




# load the stringr library
library(stringr)
  
# combine 5 strings without separator
print(str_c("Welcome","to","Beginner",
            "for","Beginner")) 


Output:

[1] "Welcometow3wiki"

Example 2:

In this example, we are concatenating 5 strings by different separators.

R




# load the stringr library
library(stringr)
  
# combine 5 strings with comma separator
print(str_c("Welcome","to","Beginner",
            "for","Beginner",sep=",")) 
  
# combine 5 strings with space separator
print(str_c("Welcome","to","Beginner",
            "for","Beginner",sep=" ")) 
  
# combine 5 strings with & separator
print(str_c("Welcome","to","Beginner",
            "for","Beginner",sep="&"))


[1] "Welcome,to,Beginner,for,Beginner"
[1] "Welcome to Beginner for Beginner"
[1] "Welcome&to&Beginner&for&Beginner"


Contact Us