Title: | 'Python' Style String Functions |
---|---|
Description: | A comprehensive set of string manipulation functions based on those found in 'Python' without relying on 'reticulate'. It provides functions that intend to (1) make it easier for users familiar with 'Python' to work with strings, (2) reduce the complexity often associated with string operations, (3) and enable users to write more readable and maintainable code that manipulates strings. |
Authors: | Garrett Shipley [aut, cre] |
Maintainer: | Garrett Shipley <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2025-02-12 06:12:10 UTC |
Source: | https://github.com/pythonicr/strs |
This function capitalizes the first character of each string in a given
string, based on the specified locale. This is similar to Python's
str.capitalize()
method.
strs_capitalize(string, locale = "en")
strs_capitalize(string, locale = "en")
string |
A character vector where each element is a string to be capitalized. |
locale |
A character string representing the locale to be used for capitalization. Defaults to "en" (English). The locale affects the rules for identifying sentences in the string. |
A character vector of the same length as string
, where each element
is the capitalized version of the corresponding element in string
.
Python str.capitalize() documentation
strs_capitalize("hello world")
strs_capitalize("hello world")
strs_casefold
is used to perform case folding on each element of a
character vector. This function is particularly useful for case-insensitive
string matching and is similar to Python's str.casefold()
method.
strs_casefold(string)
strs_casefold(string)
string |
A character vector where each element is a string to be case-folded. |
A character vector of the same length as string
, where each element
has been case-folded.
Python str.casefold() documentation
strs_casefold("HELLO World") strs_casefold("Äpfel")
strs_casefold("HELLO World") strs_casefold("Äpfel")
strs_center
centers each element of a character vector in a field of a
specified width. It pads the string on both sides with a specified character
(defaulting to a space). This is similar to Python's str.center()
method.
strs_center(string, width, fillchar = " ")
strs_center(string, width, fillchar = " ")
string |
A character vector where each element is a string to be centered. |
width |
The total width of the field in which the string is to be centered. |
fillchar |
A character used for padding. If not specified, defaults to a
space. Only the first character of |
A character vector of the same length as string
, where each element
has been centered in a field of the specified width.
Python str.center() documentation
strs_center("hello", 10) strs_center("world", 10, "*")
strs_center("hello", 10) strs_center("world", 10, "*")
strs_contains
checks whether each element of a character vector contains a
specified substring. This function mirrors the functionality of Python's
str.__contains__()
method.
strs_contains(string, substring)
strs_contains(string, substring)
string |
A character vector where each element is a string to be checked. |
substring |
The substring to search for within each element of |
A logical vector of the same length as string
, with each element
indicating whether the corresponding element of string
contains
substring
.
strs_contains("hello world", "world") strs_contains(c("apple", "banana", "cherry"), "a")
strs_contains("hello world", "world") strs_contains(c("apple", "banana", "cherry"), "a")
strs_count
counts the number of times a specified substring occurs in each
element of a character vector. Optionally, the search can be limited to a
substring of each element, specified by start
and end
positions. This
function is similar to Python's str.count()
method.
strs_count(string, substring, start = 1L, end = -1L)
strs_count(string, substring, start = 1L, end = -1L)
string |
A character vector where each element is a string in which to
count occurrences of |
substring |
The substring to count within each element of |
start |
An optional integer specifying the starting position in each
element of |
end |
An optional integer specifying the ending position in each element
of |
An integer vector of the same length as string
, with each element
indicating the count of substring
in the corresponding element of string
.
Python str.count() documentation
strs_count("hello world", "o") strs_count("banana", "na") strs_count("hello world", "o", start = 6) strs_count("hello world", "o", end = 5)
strs_count("hello world", "o") strs_count("banana", "na") strs_count("hello world", "o", start = 6) strs_count("hello world", "o", end = 5)
strs_endswith
determines whether each element of a character vector ends
with a specified suffix. This function is similar to Python's
str.endswith()
method.
strs_endswith(string, suffix)
strs_endswith(string, suffix)
string |
A character vector where each element is a string to be checked. |
suffix |
The suffix to check for at the end of each element of |
A logical vector of the same length as string
, with each element
indicating whether the corresponding element of string
ends with suffix
.
Python str.endswith() documentation
strs_endswith("hello world", "world") strs_endswith(c("test", "hello", "world"), "ld")
strs_endswith("hello world", "world") strs_endswith(c("test", "hello", "world"), "ld")
strs_expandtabs
replaces each tab character (\\t
) in a string with a
specified number of spaces. This function behaves similarly to Python's
str.expandtabs()
method.
strs_expandtabs(string, tabsize = 8)
strs_expandtabs(string, tabsize = 8)
string |
A character vector where each element is a string in which to expand tabs. |
tabsize |
An integer specifying the number of spaces to replace each tab character with. Defaults to 8. |
A character vector of the same length as string
, with tabs in each
element replaced by tabsize
number of spaces.
Python str.expandtabs() documentation
strs_expandtabs("hello\tworld", 4) strs_expandtabs("one\ttwo\tthree", 8)
strs_expandtabs("hello\tworld", 4) strs_expandtabs("one\ttwo\tthree", 8)
The strs_f
function allows for inline variable interpolation in strings, similar to Python's f-strings.
strs_f(string)
strs_f(string)
string |
A character vector containing the string with placeholders in curly braces |
Under the hood, this function uses the glue::glue_data()
function to perform the interpolation. However,
this function doesn't let you change the evaluation context or perform any string trimming. If any interpolated
expression is NA, it will be NA in the output. If any interpolated expression is NULL, it will be NULL in the output.
A character vector with the placeholders replaced by the evaluated expressions.
name <- "Alice" age <- 30 strs_f("My name is {name} and I am {age} years old.") # Output: "My name is Alice and I am 30 years old." age <- NULL strs_f("My name is {name} and I am {age} years old.") # Output: "My name is Alice and I am NULL years old."
name <- "Alice" age <- 30 strs_f("My name is {name} and I am {age} years old.") # Output: "My name is Alice and I am 30 years old." age <- NULL strs_f("My name is {name} and I am {age} years old.") # Output: "My name is Alice and I am NULL years old."
strs_find
locates the first occurrence of a specified substring within each
element of a character vector. This function is analogous to Python's
str.find()
method.
strs_find(string, substring)
strs_find(string, substring)
string |
A character vector where each element is a string to search. |
substring |
The substring to find within each element of |
An integer vector of the same length as string
, with each element
representing the starting position of the first occurrence of substring
in
the corresponding element of string
. If the substring is not found, the
function returns NA for that element.
Python str.find() documentation
strs_find("hello world", "world") strs_find("hello world", "x")
strs_find("hello world", "world") strs_find("hello world", "x")
The strs_format
function provides string interpolation using values from a data argument, similar to Python's
str.format
method. For more information, see the details.
strs_format(string, ..., .data = NULL)
strs_format(string, ..., .data = NULL)
string |
A character vector containing the string with placeholders in curly braces |
... |
Named arguments that will be made available during |
.data |
List-like (e.g., data.frame, list, environment, etc.) object that will be used for lookups during
|
Under the hood, this function uses the glue::glue_data()
function to perform the interpolation. By
default, this function will only use the variables provided in ...
and .data
. It will NOT use the immediate
environment to perform the interpolations. If any interpolated expression is NA, it will be NA in the output. If any
interpolated expression is NULL, it will be NULL in the output.
A character vector with the placeholders replaced by the evaluated expressions.
# Using a named list for substitutions data_list <- list(name = "Alice", age = 30) strs_format("My name is {name} and I am {age} years old.", .data = data_list) # Output: "My name is Alice and I am 30 years old." # Using additional arguments strs_format("My name is {name} and I am {age} years old.", name = "Bob", age = 25) # Output: "My name is Bob and I am 25 years old."
# Using a named list for substitutions data_list <- list(name = "Alice", age = 30) strs_format("My name is {name} and I am {age} years old.", .data = data_list) # Output: "My name is Alice and I am 30 years old." # Using additional arguments strs_format("My name is {name} and I am {age} years old.", name = "Bob", age = 25) # Output: "My name is Bob and I am 25 years old."
strs_isalnum
checks whether each element of a character vector is
alphanumeric. This means that the function tests if all characters in the
string are either letters or digits. It is similar to Python's
str.isalnum()
method.
strs_isalnum(string)
strs_isalnum(string)
string |
A character vector to be checked. |
A logical vector of the same length as string
, with each element
indicating whether the corresponding element of string
is completely
alphanumeric.
Python str.isalnum() documentation
strs_isalnum("hello123") strs_isalnum("hello world") strs_isalnum("12345")
strs_isalnum("hello123") strs_isalnum("hello world") strs_isalnum("12345")
strs_isalpha
checks whether each element of a character vector contains
only alphabetical characters. It is similar to Python's str.isalpha()
method.
strs_isalpha(string)
strs_isalpha(string)
string |
A character vector to be checked. |
A logical vector of the same length as string
, indicating whether
each element contains only alphabetical characters.
Python str.isalpha() documentation
strs_isalpha("hello") strs_isalpha("hello123")
strs_isalpha("hello") strs_isalpha("hello123")
strs_isascii
determines whether each element of a character vector contains
only ASCII characters. It is similar to Python's str.isascii()
method.
strs_isascii(string)
strs_isascii(string)
string |
A character vector to be checked. |
A logical vector of the same length as string
, indicating whether
each element contains only ASCII characters.
Python str.isascii() documentation
strs_isascii("hello") strs_isascii("héllo")
strs_isascii("hello") strs_isascii("héllo")
strs_isdecimal
checks whether each element of a character vector contains
only decimal characters. It is similar to Python's str.isdecimal()
method.
strs_isdecimal(string)
strs_isdecimal(string)
string |
A character vector to be checked. |
A logical vector of the same length as string
, indicating whether
each element contains only decimal characters.
Python str.isdecimal() documentation
strs_isdecimal("12345") strs_isdecimal("123.45") # FALSE
strs_isdecimal("12345") strs_isdecimal("123.45") # FALSE
strs_isdigit
checks whether each element of a character vector contains
only digits. It is similar to Python's str.isdigit()
method.
strs_isdigit(string)
strs_isdigit(string)
string |
A character vector to be checked. |
A logical vector of the same length as string
, indicating whether
each element contains only digits.
Python str.isdigit() documentation
strs_isdigit("12345") strs_isdigit("123a")
strs_isdigit("12345") strs_isdigit("123a")
strs_islower
checks whether each element of a character vector is in
lowercase. It is similar to Python's str.islower()
method.
strs_islower(string)
strs_islower(string)
string |
A character vector to be checked. |
A logical vector of the same length as string
, indicating whether
each element is entirely in lowercase.
Python str.islower() documentation
strs_islower("hello") strs_islower("Hello")
strs_islower("hello") strs_islower("Hello")
strs_isnumeric
checks whether each element of a character vector contains
only numeric characters. It is similar to Python's str.isnumeric()
method.
strs_isnumeric(string)
strs_isnumeric(string)
string |
A character vector to be checked. |
A logical vector of the same length as string
, indicating whether
each element contains only numeric characters.
Python str.isnumeric() documentation
strs_isnumeric("12345") strs_isnumeric("123a") # contains a non-numeric character
strs_isnumeric("12345") strs_isnumeric("123a") # contains a non-numeric character
strs_isspace
checks whether each element of a character vector contains
only whitespace characters. It is similar to Python's str.isspace()
method.
strs_isspace(string)
strs_isspace(string)
string |
A character vector to be checked. |
A logical vector of the same length as string
, indicating whether
each element contains only whitespace characters.
Python str.isspace() documentation
strs_isspace(" ") strs_isspace("hello world")
strs_isspace(" ") strs_isspace("hello world")
strs_istitle
checks whether each element of a character vector is title
case. This is similar to Python's str.istitle
method.
strs_istitle(string)
strs_istitle(string)
string |
A character vector where each element is a string to be checked. |
A logical vector of the same length as string
, indicating whether
each element is in title case.
Python str.istitle() documentation
strs_istitle("This Is Title Case") strs_istitle("not title case") strs_istitle("123 Another Example")
strs_istitle("This Is Title Case") strs_istitle("not title case") strs_istitle("123 Another Example")
strs_isupper
checks whether each element of a character vector is in
uppercase. It is similar to Python's str.isupper()
method.
strs_isupper(string)
strs_isupper(string)
string |
A character vector to be checked. |
A logical vector of the same length as string
, indicating whether
each element is entirely in uppercase.
Python str.isupper() documentation
strs_isupper("HELLO") strs_isupper("Hello")
strs_isupper("HELLO") strs_isupper("Hello")
strs_join
concatenates elements of iterable
using sep
. It is similar to
Python's str.join()
.
strs_join(sep, iterable)
strs_join(sep, iterable)
sep |
A string separator used to join the elements. |
iterable |
A character vector to be joined. |
A single string with elements of iterable
joined by sep
.
Python str.join() documentation
strs_join("-", c("hello", "world")) strs_join("", c("hello", "world")) # no separator
strs_join("-", c("hello", "world")) strs_join("", c("hello", "world")) # no separator
strs_len
returns the number of characters in string
. It is similar to
Python's str.__len__()
.
strs_len(string)
strs_len(string)
string |
A character vector of strings. |
A numeric vector where each element corresponds to the number of characters.
Python str.len() documentation
strs_len(c("hello", "world"))
strs_len(c("hello", "world"))
strs_ljust
left-justifies each element of a character vector in a field of
a specified width. It is similar to Python's str.ljust()
method.
strs_ljust(string, width, fillchar = " ")
strs_ljust(string, width, fillchar = " ")
string |
A character vector where each element is a string to be left-justified. |
width |
The total width of the field in which the string is to be left-justified. |
fillchar |
A character used for padding on the right. |
A character vector of the same length as string
, with each element
left-justified in a field of the specified width.
Python str.ljust() documentation
strs_ljust("hello", 10) strs_ljust("world", 10, "*")
strs_ljust("hello", 10) strs_ljust("world", 10, "*")
strs_lower
converts each element of a character vector to lowercase, based
on the specified locale. It is similar to Python's str.lower()
method.
strs_lower(string, locale = "en")
strs_lower(string, locale = "en")
string |
A character vector to be converted to lowercase. |
locale |
A character string representing the locale to be used for the conversion. |
A character vector of the same length as string
, with each element
converted to lowercase.
Python str.lower() documentation
strs_lower("HELLO WORLD") strs_lower("Äpfel", locale = "de")
strs_lower("HELLO WORLD") strs_lower("Äpfel", locale = "de")
strs_lstrip
removes leading characters (spaces by default) from each
element of a character vector. It is similar to Python's str.lstrip()
method.
strs_lstrip(string, chars = NULL)
strs_lstrip(string, chars = NULL)
string |
A character vector where each element is a string to be left-stripped. |
chars |
An optional string of characters to be removed from the beginning of each element. If NULL, whitespace is removed. |
A character vector of the same length as string
, with specified
characters removed from the beginning of each element.
Python str.lstrip() documentation
strs_lstrip(" hello world") strs_lstrip("xxxyhello world", chars = "xy")
strs_lstrip(" hello world") strs_lstrip("xxxyhello world", chars = "xy")
strs_normalize_whitespace
normalizes the whitespace in each element of a
character vector. It trims leading and trailing whitespace and replaces any
sequence of whitespace characters within the string with a single space. This
function is akin to the typical Python pattern " ".join(str.split())
.
strs_normalize_whitespace(string)
strs_normalize_whitespace(string)
string |
A character vector where each element is a string in which to normalize whitespace. |
A character vector of the same length as string
, with whitespace
normalized in each element.
strs_normalize_whitespace(" hello world ") strs_normalize_whitespace("\thello\nworld\t")
strs_normalize_whitespace(" hello world ") strs_normalize_whitespace("\thello\nworld\t")
strs_removeprefix
removes a specified prefix from the start of each element
of a character vector. It is similar to Python's str.removeprefix()
method.
strs_removeprefix(string, prefix)
strs_removeprefix(string, prefix)
string |
A character vector where each element is a string from which to remove the prefix. |
prefix |
The prefix to remove. |
A character vector of the same length as string
, with the prefix
removed from each element.
Python str.removeprefix() documentation
strs_removeprefix("testString", "test") strs_removeprefix("hello world", "hello")
strs_removeprefix("testString", "test") strs_removeprefix("hello world", "hello")
strs_removesuffix
removes a specified suffix from the end of each element
of a character vector. It is similar to Python's str.removesuffix()
method.
strs_removesuffix(string, suffix)
strs_removesuffix(string, suffix)
string |
A character vector where each element is a string from which to remove the suffix. |
suffix |
The suffix to remove. |
A character vector of the same length as string
, with the suffix
removed from each element.
Python str.removesuffix() documentation
strs_removesuffix("StringTest", "Test") strs_removesuffix("hello world", "world")
strs_removesuffix("StringTest", "Test") strs_removesuffix("hello world", "world")
strs_replace
replaces all occurrences of a specified substring in each
element of a character vector. It is similar to Python's str.replace()
method.
strs_replace(string, substring, replacement)
strs_replace(string, substring, replacement)
string |
A character vector where each element is a string in which to
replace |
substring |
The substring to be replaced. |
replacement |
The string to replace |
A character vector of the same length as string
, with substring
replaced by replacement
.
Python str.replace() documentation
strs_replace("hello world", "world", "there") strs_replace("banana", "na", "mo")
strs_replace("hello world", "world", "there") strs_replace("banana", "na", "mo")
strs_rfind
locates the last occurrence of a specified substring within each
element of a character vector. It is similar to Python's str.rfind()
method.
strs_rfind(string, substring)
strs_rfind(string, substring)
string |
A character vector where each element is a string to search. |
substring |
The substring to find within each element of |
An integer vector of the same length as string
, with each element
representing the starting position of the last occurrence of substring
in
the corresponding element of string
. If the substring is not found, the
function returns NA for that element.
Python str.rfind() documentation
strs_rfind("hello world", "o") strs_rfind("hello world", "x") # not found
strs_rfind("hello world", "o") strs_rfind("hello world", "x") # not found
strs_rjust
right-justifies each element of a character vector in a field of
a specified width. It is similar to Python's str.rjust()
method.
strs_rjust(string, width, fillchar = " ")
strs_rjust(string, width, fillchar = " ")
string |
A character vector where each element is a string to be right-justified. |
width |
The total width of the field in which the string is to be right-justified. |
fillchar |
A character used for padding on the left. |
A character vector of the same length as string
, with each element
right-justified in a field of the specified width.
Python str.rjust() documentation
strs_rjust("hello", 10) strs_rjust("world", 10, "*")
strs_rjust("hello", 10) strs_rjust("world", 10, "*")
strs_rstrip
removes trailing characters (spaces by default) from each
element of a character vector. It is similar to Python's str.rstrip()
method.
strs_rstrip(string, chars = NULL)
strs_rstrip(string, chars = NULL)
string |
A character vector where each element is a string to be right-stripped. |
chars |
An optional string of characters to be removed from the end of each element. If NULL, whitespace is removed. |
A character vector of the same length as string
, with specified
characters removed from the end of each element.
Python str.rstrip() documentation
strs_rstrip("hello world ") strs_rstrip("hello worldxxx", chars = "x")
strs_rstrip("hello world ") strs_rstrip("hello worldxxx", chars = "x")
strs_slice
extracts substrings from each element of a character vector,
specified by start and stop positions. It is similar to Python's slicing
syntax for strings, but it uses 1 indexing and stops are inclusive.
strs_slice(string, start = 1L, stop = -1L, ..., step = 1L)
strs_slice(string, start = 1L, stop = -1L, ..., step = 1L)
string |
A character vector where each element is a string to slice. |
start |
An integerish scalar for the starting position for slicing (inclusive). |
stop |
An integerish scalar for the ending position for slicing (inclusive). |
... |
Used to force keyword argument usage of |
step |
An integer greater than 0 or equal to -1 for the step size. If -1 is provided, each string will be reversed after slicing operations. |
A character vector of the same length as string
, with each element
being the sliced substring.
strs_slice("hello world", 1, 5) strs_slice("hello world", 7) strs_slice("hello world", start = 7, stop = 11)
strs_slice("hello world", 1, 5) strs_slice("hello world", 7) strs_slice("hello world", start = 7, stop = 11)
strs_split
splits each element of a character vector into substrings based
on a separator. It is similar to Python's str.split()
method.
strs_split(string, sep = " ", maxsplit = -1L)
strs_split(string, sep = " ", maxsplit = -1L)
string |
A character vector to split. |
sep |
The separator on which to split the string. |
maxsplit |
The maximum number of splits to perform. If -1, all possible splits are performed. |
A list of character vectors, with each vector containing the split
substrings from the corresponding element of string
.
Python str.split() documentation
strs_split("hello world", " ") strs_split("one,two,three", ",", maxsplit = 1)
strs_split("hello world", " ") strs_split("one,two,three", ",", maxsplit = 1)
strs_splitlines
splits each element of a character vector into separate
lines. It is similar to Python's str.splitlines()
method.
strs_splitlines(string, keepends = FALSE)
strs_splitlines(string, keepends = FALSE)
string |
A character vector to be split into lines. |
keepends |
A boolean indicating whether to retain line end characters. |
A list of character vectors, with each vector containing lines from
the corresponding element of string
.
Python str.splitlines() documentation
strs_splitlines("hello\nworld\n") strs_splitlines("line1\r\nline2\n", keepends = TRUE)
strs_splitlines("hello\nworld\n") strs_splitlines("line1\r\nline2\n", keepends = TRUE)
strs_startswith
determines whether each element of a character vector
starts with a specified prefix. It is similar to Python's str.startswith()
method.
strs_startswith(string, prefix)
strs_startswith(string, prefix)
string |
A character vector where each element is a string to be checked. |
prefix |
The prefix to check for at the start of each element of
|
A logical vector of the same length as string
, with each element
indicating whether the corresponding element of string
starts with
prefix
.
Python str.startswith() documentation
strs_startswith("hello world", "hello") strs_startswith(c("test", "hello", "world"), "te")
strs_startswith("hello world", "hello") strs_startswith(c("test", "hello", "world"), "te")
strs_strip
removes leading and trailing characters (spaces by default) from
each element of a character vector. It is similar to Python's str.strip()
method.
strs_strip(string, chars = NULL)
strs_strip(string, chars = NULL)
string |
A character vector where each element is a string to be stripped. |
chars |
An optional string of characters to be removed from both ends of each element. If NULL, whitespace is removed. |
A character vector of the same length as string
, with specified
characters removed from both ends of each element.
Python str.strip() documentation
strs_strip(" hello world ") strs_strip("xxxyhello worldyyy", chars = "xy")
strs_strip(" hello world ") strs_strip("xxxyhello worldyyy", chars = "xy")
strs_swapcase
returns a copy of the string with uppercase characters convert
to lowercase and visa-versa. It is similar to Python's str.swapcase()
.
strs_swapcase(string)
strs_swapcase(string)
string |
A character vector where each element is a string. |
A character vector of the same length as string
, with specified
uppercase characters converted to lowercase and visa-versa.
Python str.swapcase() documentation
strs_swapcase("Hello World")
strs_swapcase("Hello World")
strs_title
converts each element of a character vector to title case, based
on the specified locale. It is similar to Python's str.title()
method.
strs_title(string, locale = "en")
strs_title(string, locale = "en")
string |
A character vector to be converted to title case. |
locale |
A character string representing the locale to be used for the conversion. |
A character vector of the same length as string
, with each element
converted to title case.
Python str.title() documentation
strs_title("hello world") strs_title("guten tag", locale = "de")
strs_title("hello world") strs_title("guten tag", locale = "de")
strs_upper
converts each element of a character vector to uppercase, based
on the specified locale. It is similar to Python's str.upper()
method.
strs_upper(string, locale = "en")
strs_upper(string, locale = "en")
string |
A character vector to be converted to uppercase. |
locale |
A character string representing the locale to be used for the conversion. |
A character vector of the same length as string
, with each element
converted to uppercase.
Python str.upper() documentation
strs_upper("hello world") strs_upper("äpfel", locale = "de")
strs_upper("hello world") strs_upper("äpfel", locale = "de")