Overview
strs is an R package that provides a comprehensive set of string manipulation functions, mirroring the functionality and naming conventions of Python’sstr methods. It aims to make string operations in R more accessible for users familiar with Python. Under the hood, every function uses thestringi package to ensure the results are consistent.
Installation
You can install thestrs package directly from GitHub.
# Install devtools if you haven't alreadyinstall.packages("devtools")# Install strs package from GitHubdevtools::install_github("pythonicr/strs")# Using pacmanpacman::p_load_gh("pythonicr/strs")Getting Started
Basic Usage
Here are some examples demonstrating how to use the functions provided by thestrs package.
Capitalize the First Character of Each Sentence
Thestrs_capitalize function capitalizes the first character of each string in a character vector.
library(strs)# Capitalize the first character of each sentencecapitalized<-strs_capitalize("hello world")print(capitalized)#> [1] "Hello world"Perform Case Folding on Strings
Thestrs_casefold function performs case folding on each element of a character vector, useful for case-insensitive matching.
# Perform case foldingfolded<-strs_casefold("HELLO World")print(folded)#> [1] "hello world"Center a String in a Field of a Given Width
Thestrs_center function centers each element of a character vector in a field of a specified width, padding with a specified character.
# Center a string with paddingcentered<-strs_center("hello",10)print(centered)#> [1] " hello "Check if String Contains a Substring
Thestrs_contains function checks whether each element of a character vector contains a specified substring.
# Check if strings contain a substringcontains<-strs_contains("hello world","world")print(contains)#> [1] TRUECount Occurrences of a Substring in a String
Thestrs_count function counts the number of times a specified substring occurs in each element of a character vector.
# Count occurrences of a substringcount<-strs_count("hello world","o")print(count)#> [1] 2Check if String Ends With a Specified Suffix
Thestrs_endswith function determines whether each element of a character vector ends with a specified suffix.
# Check if strings end with a suffixendswith<-strs_endswith("hello world","world")print(endswith)#> [1] TRUEExpand Tabs in a String to Spaces
Thestrs_expandtabs function replaces each tab character (\t) in a string with a specified number of spaces.
# Expand tabs to spacesexpanded<-strs_expandtabs("hello\tworld",4)print(expanded)#> [1] "hello world"Find the First Occurrence of a Substring in a String
Thestrs_find function locates the first occurrence of a specified substring within each element of a character vector.
Check if String is Alphanumeric
Thestrs_isalnum function checks whether each element of a character vector is alphanumeric.
# Check if strings are alphanumericisalnum<-strs_isalnum("hello123")print(isalnum)#> [1] TRUECheck if String Contains Only Alphabetical Characters
Thestrs_isalpha function checks whether each element of a character vector contains only alphabetical characters.
# Check if strings are alphabeticalisalpha<-strs_isalpha("hello")print(isalpha)#> [1] TRUECheck if String Contains Only ASCII Characters
Thestrs_isascii function determines whether each element of a character vector contains only ASCII characters.
# Check if strings are ASCIIisascii<-strs_isascii("hello")print(isascii)#> [1] TRUECheck if String Contains Only Decimal Characters
Thestrs_isdecimal function checks whether each element of a character vector contains only decimal characters.
# Check if strings are decimalisdecimal<-strs_isdecimal("12345")print(isdecimal)#> [1] TRUECheck if String Contains Only Digits
Thestrs_isdigit function checks whether each element of a character vector contains only digits.
# Check if strings are digitsisdigit<-strs_isdigit("12345")print(isdigit)#> [1] TRUECheck if String is in Lowercase
Thestrs_islower function checks whether each element of a character vector is in lowercase.
# Check if strings are lowercaseislower<-strs_islower("hello")print(islower)#> [1] TRUECheck if String Contains Only Numeric Characters
Thestrs_isnumeric function checks whether each element of a character vector contains only numeric characters.
# Check if strings are numericisnumeric<-strs_isnumeric("12345")print(isnumeric)#> [1] TRUECheck if String Contains Only Whitespace Characters
Thestrs_isspace function checks whether each element of a character vector contains only whitespace characters.
# Check if strings are whitespaceisspace<-strs_isspace(" ")print(isspace)#> [1] TRUECheck if String is in Title Case
Thestrs_istitle function checks whether each element of a character vector is title case.
# Check if strings are title caseistitle<-strs_istitle("This Is Title Case")print(istitle)#> [1] TRUECheck if String is in Uppercase
Thestrs_isupper function checks whether each element of a character vector is in uppercase.
# Check if strings are uppercaseisupper<-strs_isupper("HELLO")print(isupper)#> [1] TRUEJoin Elements into a Single String with a Separator
Thestrs_join function concatenates elements of an iterable using a separator.
Left-justify String in a Field of a Given Width
Thestrs_ljust function left-justifies each element of a character vector in a field of a specified width.
# Left-justify a stringljust<-strs_ljust("hello",10)print(ljust)#> [1] " hello"Convert String to Lowercase
Thestrs_lower function converts each element of a character vector to lowercase, based on the specified locale.
# Convert strings to lowercaselower<-strs_lower("HELLO WORLD")print(lower)#> [1] "hello world"Left Strip Characters from a String
Thestrs_lstrip function removes leading characters (spaces by default) from each element of a character vector.
# Left-strip characterslstrip<-strs_lstrip(" hello world")print(lstrip)#> [1] "hello world"Normalize Whitespace in a String
Thestrs_normalize_whitespace function normalizes the whitespace in each element of a character vector.
# Normalize whitespacenormalized<-strs_normalize_whitespace(" hello world ")print(normalized)#> [1] "hello world"Remove a Prefix from a String
Thestrs_removeprefix function removes a specified prefix from the start of each element of a character vector.
# Remove a prefixremoved_prefix<-strs_removeprefix("testString","test")print(removed_prefix)#> [1] "String"Remove a Suffix from a String
Thestrs_removesuffix function removes a specified suffix from the end of each element of a character vector.
# Remove a suffixremoved_suffix<-strs_removesuffix("StringTest","Test")print(removed_suffix)#> [1] "String"Replace Substring in a String
Thestrs_replace function replaces all occurrences of a specified substring in each element of a character vector.
# Replace a substringreplaced<-strs_replace("hello world","world","there")print(replaced)#> [1] "hello there"Find the Last Occurrence of a Substring in a String
Thestrs_rfind function locates the last occurrence of a specified substring within each element of a character vector.
# Find the last occurrence of a substringlast_occurrence<-strs_rfind("hello world","o")print(last_occurrence)#> [1] 8# 8Right-justify String in a Field of a Given Width
Thestrs_rjust function right-justifies each element of a character vector in a field of a specified width.
# Right-justify a stringrjust<-strs_rjust("hello",10)print(rjust)#> [1] "hello "Right Strip Characters from a String
Thestrs_rstrip function removes trailing characters (spaces by default) from each element of a character vector.
# Right-strip charactersrstrip<-strs_rstrip("hello world ")print(rstrip)#> [1] "hello world"Slice Substrings from a String
Thestrs_slice function extracts substrings from each element of a character vector, specified by start and stop positions.
# Slice substringssliced<-strs_slice("hello world",1,5)print(sliced)#> [1] "hello"Split String into Substrings
Thestrs_split function splits each element of a character vector into substrings based on a separator.
# Split strings into substringssplit<-strs_split("hello world"," ")print(split)# list("hello", "world")#> [[1]]#> [1] "hello" "world"Split String into Lines
Thestrs_splitlines function splits each element of a character vector into separate lines.
# Split strings into linessplit_lines<-strs_splitlines("hello\nworld\n")print(split_lines)# list("hello", "world")#> [[1]]#> [1] "hello" "world"Check if String Starts With a Specified Prefix
Thestrs_startswith function determines whether each element of a character vector starts with a specified prefix.
# Check if strings start with a prefixstartswith<-strs_startswith("hello world","hello")print(startswith)#> [1] TRUEStrip Characters from Both Ends of a String
Thestrs_strip function removes leading and trailing characters (spaces by default) from each element of a character vector.
# Strip characters from both endsstrip<-strs_strip(" hello world ")print(strip)#> [1] "hello world"Convert String to Title Case
Thestrs_title function converts each element of a character vector to title case, based on the specified locale.
# Convert strings to title casetitle<-strs_title("hello world")print(title)#> [1] "Hello World"Convert String to Uppercase
Thestrs_upper function converts each element of a character vector to uppercase, based on the specified locale.
# Convert strings to uppercaseupper<-strs_upper("hello world")print(upper)#> [1] "HELLO WORLD"