连接函数paste,用于连接字符
#paste function
#join together two or more strings into one string
paste("I","love","R language.")
paste("I","love","English.",sep="-")
paste("I","love","English.",sep="")
paste("1+1=",2)
t1=c("Jim","Sunny","Evan")
paste(t1,"love","Maths")
#substr function
#If you want to extract a portion of a string according to position.
#you can use the substr(string,start,end),it can extract the substring that begins at start and ends at end.
str="ABCDEFG"
substr(str,1,4)#return "ABCD"
substr(str,3,6)#return "CDEF"
str1=c("Test","Yes","Thanks")
substr(str1,1,3) # return "Tes" "Yes" "Tha"
cities=c("New York, NY", "Los Angeles, CA", "Peoria, IL")
substr(cities, nchar(cities)-1, nchar(cities))
#strsplit function
#If you want to split a string into substrings,you can use it.
#strsplit(string, delimiter)
str="home/mike/data/trials.csv"
strsplit(str,"/") #return each substring
str1=c("home/mike/data/trials.csv","home/mike/data/errors.csv","home/mike/corr/reject.doc")
strsplit(str1,"/")
#Replacing Substrings
#Use sub to replace the first instance of a substring
#sub(old,new,string)
#use gsub to replace all instances of a substring
#gsub(old,new,string)
str="I hate USA.I love dancing."
sub("I","You",str)
gsub("I","You",str)