1. Strings

Introduction

#-----------------------------
string <- '\n'                    # a "newline" character 
string <- '\\n'                   # two characters, \ and an n
#-----------------------------
string <- "\n"                    # a "newline" character
string <- "\\n"                   # two characters, \ and an n
#-----------------------------
string <- "Jon 'Maddog' Orwant"   # literal single quotes
string <- 'Jon \'Maddog\' Orwant' # literal single quotes
#-----------------------------
string <- 'Jon "Maddog" Orwant'   # literal double quotes
string <- "Jon \"Maddog\" Orwant" # literal double quotes
#-----------------------------
string <- "This is a multiline string which will include a
newline character"
#-----------------------------
string <- paste(
"This is also a multiline",
" string made from",
" concatenating a string list"
,sep="")
#-----------------------------

Accessing Substrings

#-----------------------------
value <- substr   (string, start, stop)
value <- substring(string, start, stop)
value <- substring(string, start)
#-----------------------------
# Can only replace up to the same size string
substring(string, start, stop) <- newstring
#-----------------------------
string <- "This is what you have"

first  <- substr   (string,1,1) # "T"
first  <- substring(string,1,1) # "T"
start  <- substr   (string,6,7) # "is"
start  <- substring(string,6,7) # "is"
rest   <- substring(string, 14) # "you have"
#-----------------------------
# Negative indexing not explicitly supported
last   <- substring(string,nchar(string))                   # "e"
end    <- substring(string,nchar(string)-3)                 # "have"
piece  <- substr   (string,nchar(string)-7,nchar(string)-5) # "you"
piece  <- substring(string,nchar(string)-7,nchar(string)-5) # "you"
#-----------------------------
string <- "This is what you have"
string
string <- 'Jon \'Maddog\' Orwant\n' # literal single quotes
string                              # "Jon 'Maddog' Orwant\n"
cat(string)                         # Jon 'Maddog' Orwant
#-----------------------------
substr(string,6,7) <- "be"
# This be what you have

string <- paste(substr(string,1,5),"wasn't",substring(string,8),sep="")

string <- paste(substr(string,1,nchar(string)-12),"ondrous",sep="") # replace last 12 characters
#This wasn't wondrous

string <- substr(string,2,nchar(string)) # delete first character
#his wasn't wondrous

string <- substr(string,1,nchar(string)-10) # delete last 10 characters
#his wasn'
#-----------------------------
# You can test substrings
if ( any(grep("pattern",substring(string,10))) )
  cat("Pattern matches in last 10 characters\n")

if ( any(regexpr("pattern",substring(string,10))>0) )
  cat("Pattern matches in last 10 characters\n")

# substitute "at" for "is", restricted to first five characters
substr(string, 1, 5) <- gsub("is","at",string)

#-----------------------------

Establishing a Default Value

#-----------------------------
a <- if ( exists("b") ) b else c
#-----------------------------
dir <- if ( exists(commandArgs()[2]) ) commandArgs()[2] else "/tmp"
#-----------------------------
user <- Sys.getenv("USER")
user <- Sys.getenv("LOGNAME")
user <- Sys.info()[7]
user <- Sys.info()["user"]
user <- Sys.info()[6]
user <- Sys.info()["login"]

Exchanging Values Without Using Temporary Variables

#-----------------------------
# No built in way to swap without a temporary variable
#-----------------------------
temp <- a ; a <- b ; b <- temp
#-----------------------------

Converting Between ASCII Characters and Values

#-----------------------------
num  <- as.integer(charToRaw(char))
char <- rawToChar(as.raw(num))
#-----------------------------
char <- sprintf("%s", rawToChar(as.raw(num)))
sprintf("Number %d is character %s", num, rawToChar(as.raw(num)))
# Number 101 is character e
#-----------------------------
ascii  <- as.integer(charToRaw(string))
string <- rawToChar(as.raw(ascii))
#-----------------------------
ascii_value <- as.integer(charToRaw("e")) # now 101
character   <- rawToChar(as.raw(101))     # now "e"
#-----------------------------
sprintf("Number %d is character %s", as.integer(101), rawToChar(as.raw
(101)))
sprintf("Number %g is character %s",            101,  rawToChar(as.raw
(101)))
#-----------------------------
ascii_character_numbers <- as.integer(charToRaw("sample"))
# 115 97 109 112 108 101


word <- rawToChar(as.raw(ascii_character_numbers))
word <- rawToChar(as.raw(c(115, 97, 109, 112, 108, 101)))   # same
cat(word,"\n")
# sample
#-----------------------------
hal <- "HAL"
ascii <- as.integer(charToRaw(hal))
ascii <- ascii+1                    # add one to each ASCII value
ibm <- rawToChar(as.raw(ascii))
cat(ibm,"\n")                       # prints "IBM"
#-----------------------------

Processing a String One Character at a Time

Reversing a String by Word or Character

Expanding and Compressing Tabs

Expanding Variables in User Input

Controlling Case

Interpolating Functions and Expressions Within Strings

Indenting Here Documents

Reformatting Paragraphs

Escaping Characters

Trimming Blanks from the Ends of a String

Parsing Comma-Separated Data

Soundex Matching

Program: fixstyle

Program: psgrep