strs is an R package that provides a comprehensive
set of string manipulation functions, mirroring the functionality and
naming conventions of Python’s str
methods. It aims to make
string operations in R more accessible for users familiar with Python.
Under the hood, every function uses the stringi
package to
ensure the results are consistent.
You can install the strs
package directly from
GitHub.
# Install devtools if you haven't already
install.packages("devtools")
# Install strs package from GitHub
::install_github("pythonicr/strs") devtools
Here are some examples demonstrating how to use the functions provided by the strs package.
The strs_capitalize
function capitalizes the first
character of each string in a character vector.
library(strs)
# Capitalize the first character of each sentence
<- strs_capitalize("hello world")
capitalized print(capitalized)
#> [1] "Hello world"
The strs_casefold
function performs case folding on each
element of a character vector, useful for case-insensitive matching.
# Perform case folding
<- strs_casefold("HELLO World")
folded print(folded)
#> [1] "hello world"
The strs_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 padding
<- strs_center("hello", 10)
centered print(centered)
#> [1] " hello "
The strs_contains
function checks whether each element
of a character vector contains a specified substring.
# Check if strings contain a substring
<- strs_contains("hello world", "world")
contains print(contains)
#> [1] TRUE
The strs_count
function counts the number of times a
specified substring occurs in each element of a character vector.
# Count occurrences of a substring
<- strs_count("hello world", "o")
count print(count)
#> [1] 2
The strs_endswith
function determines whether each
element of a character vector ends with a specified suffix.
# Check if strings end with a suffix
<- strs_endswith("hello world", "world")
endswith print(endswith)
#> [1] TRUE
The strs_expandtabs
function replaces each tab character
(\t
) in a string with a specified number of spaces.
# Expand tabs to spaces
<- strs_expandtabs("hello\tworld", 4)
expanded print(expanded)
#> [1] "hello world"
The strs_find
function locates the first occurrence of a
specified substring within each element of a character vector.
# Find the first occurrence of a substring
<- strs_find("hello world", "world")
first_occurrence print(first_occurrence)
#> [1] 7
The strs_isalnum
function checks whether each element of
a character vector is alphanumeric.
# Check if strings are alphanumeric
<- strs_isalnum("hello123")
isalnum print(isalnum)
#> [1] TRUE
The strs_isalpha
function checks whether each element of
a character vector contains only alphabetical characters.
# Check if strings are alphabetical
<- strs_isalpha("hello")
isalpha print(isalpha)
#> [1] TRUE
The strs_isascii
function determines whether each
element of a character vector contains only ASCII characters.
# Check if strings are ASCII
<- strs_isascii("hello")
isascii print(isascii)
#> [1] TRUE
The strs_isdecimal
function checks whether each element
of a character vector contains only decimal characters.
# Check if strings are decimal
<- strs_isdecimal("12345")
isdecimal print(isdecimal)
#> [1] TRUE
The strs_isdigit
function checks whether each element of
a character vector contains only digits.
# Check if strings are digits
<- strs_isdigit("12345")
isdigit print(isdigit)
#> [1] TRUE
The strs_islower
function checks whether each element of
a character vector is in lowercase.
# Check if strings are lowercase
<- strs_islower("hello")
islower print(islower)
#> [1] TRUE
The strs_isnumeric
function checks whether each element
of a character vector contains only numeric characters.
# Check if strings are numeric
<- strs_isnumeric("12345")
isnumeric print(isnumeric)
#> [1] TRUE
The strs_isspace
function checks whether each element of
a character vector contains only whitespace characters.
# Check if strings are whitespace
<- strs_isspace(" ")
isspace print(isspace)
#> [1] TRUE
The strs_istitle
function checks whether each element of
a character vector is title case.
# Check if strings are title case
<- strs_istitle("This Is Title Case")
istitle print(istitle)
#> [1] TRUE
The strs_isupper
function checks whether each element of
a character vector is in uppercase.
# Check if strings are uppercase
<- strs_isupper("HELLO")
isupper print(isupper)
#> [1] TRUE
The strs_join
function concatenates elements of an
iterable using a separator.
# Join elements with a separator
<- strs_join("-", c("hello", "world"))
joined print(joined)
#> [1] "hello-world"
The strs_ljust
function left-justifies each element of a
character vector in a field of a specified width.
# Left-justify a string
<- strs_ljust("hello", 10)
ljust print(ljust)
#> [1] " hello"
The strs_lower
function converts each element of a
character vector to lowercase, based on the specified locale.
# Convert strings to lowercase
<- strs_lower("HELLO WORLD")
lower print(lower)
#> [1] "hello world"
The strs_lstrip
function removes leading characters
(spaces by default) from each element of a character vector.
# Left-strip characters
<- strs_lstrip(" hello world")
lstrip print(lstrip)
#> [1] "hello world"
The strs_normalize_whitespace
function normalizes the
whitespace in each element of a character vector.
# Normalize whitespace
<- strs_normalize_whitespace(" hello world ")
normalized print(normalized)
#> [1] "hello world"
The strs_removeprefix
function removes a specified
prefix from the start of each element of a character vector.
# Remove a prefix
<- strs_removeprefix("testString", "test")
removed_prefix print(removed_prefix)
#> [1] "String"
The strs_removesuffix
function removes a specified
suffix from the end of each element of a character vector.
# Remove a suffix
<- strs_removesuffix("StringTest", "Test")
removed_suffix print(removed_suffix)
#> [1] "String"
The strs_replace
function replaces all occurrences of a
specified substring in each element of a character vector.
# Replace a substring
<- strs_replace("hello world", "world", "there")
replaced print(replaced)
#> [1] "hello there"
The strs_rfind
function locates the last occurrence of a
specified substring within each element of a character vector.
# Find the last occurrence of a substring
<- strs_rfind("hello world", "o")
last_occurrence print(last_occurrence)
#> [1] 8
# 8
The strs_rjust
function right-justifies each element of
a character vector in a field of a specified width.
# Right-justify a string
<- strs_rjust("hello", 10)
rjust print(rjust)
#> [1] "hello "
The strs_rstrip
function removes trailing characters
(spaces by default) from each element of a character vector.
# Right-strip characters
<- strs_rstrip("hello world ")
rstrip print(rstrip)
#> [1] "hello world"
The strs_slice
function extracts substrings from each
element of a character vector, specified by start and stop
positions.
# Slice substrings
<- strs_slice("hello world", 1, 5)
sliced print(sliced)
#> [1] "hello"
The strs_split
function splits each element of a
character vector into substrings based on a separator.
# Split strings into substrings
<- strs_split("hello world", " ")
split print(split) # list("hello", "world")
#> [[1]]
#> [1] "hello" "world"
The strs_splitlines
function splits each element of a
character vector into separate lines.
# Split strings into lines
<- strs_splitlines("hello\nworld\n")
split_lines print(split_lines) # list("hello", "world")
#> [[1]]
#> [1] "hello" "world"
The strs_startswith
function determines whether each
element of a character vector starts with a specified prefix.
# Check if strings start with a prefix
<- strs_startswith("hello world", "hello")
startswith print(startswith)
#> [1] TRUE
The strs_strip
function removes leading and trailing
characters (spaces by default) from each element of a character
vector.
# Strip characters from both ends
<- strs_strip(" hello world ")
strip print(strip)
#> [1] "hello world"
The strs_title
function converts each element of a
character vector to title case, based on the specified locale.
# Convert strings to title case
<- strs_title("hello world")
title print(title)
#> [1] "Hello World"
The strs_upper
function converts each element of a
character vector to uppercase, based on the specified locale.
# Convert strings to uppercase
<- strs_upper("hello world")
upper print(upper)
#> [1] "HELLO WORLD"
We welcome contributions to the strs package. If you have suggestions, bug reports, or want to contribute code, please open an issue or submit a pull request on our GitHub repository.
strs is released under the MIT License. See the LICENSE file in the package’s repository for more details.