Open In App

str_c() Function of stringr Package in R

Improve
Improve
Like Article
Like
Save
Share
Report

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","geeks",
            "for","geeks")) 


Output:

[1] "Welcometogeeksforgeeks"

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","geeks",
            "for","geeks",sep=",")) 
  
# combine 5 strings with space separator
print(str_c("Welcome","to","geeks",
            "for","geeks",sep=" ")) 
  
# combine 5 strings with & separator
print(str_c("Welcome","to","geeks",
            "for","geeks",sep="&"))


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


Last Updated : 02 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads