the R text data helper
stringr
01 Syntax
02 Methods
| Name | Overloads | Summary |
|---|---|---|
| html | 1 | comvert any object to html text document |
| format_str | 1 | Encode in a Common Format Format an R object for pretty printing, notably encoding vector or column elements into a common format. |
| objToString | 1 | Convert an R Object to a Character String This is a helper function for format to produce a single character string describing an R object. |
| getElementFormat | 1 | |
| loadXml | 1 | load a .NET object from the xml data file |
| xml | 1 | Convert most of the R# object or VB.NET object to xml document string. |
| json | 1 | Convert most of the R# object or VB.NET object to json string. |
| base64Str | 1 | encode byte stream or text content into base64 string |
| base64Decode | 1 | decode base64 string as text or the raw bytes buffer object. |
| urldecode | 1 | Decodes URL-encoded string Decodes any %## encoding in the given string. |
| nchar | 1 | Count the Number of Characters (or Bytes or Width) nchar takes a character vector as an argument and returns a vector whose elements contain the sizes of the corresponding e… |
| regexp | 1 | Initializes a new instance of the RegularExpression class for the specified regular expression pattern. |
| match | 1 | Searches the specified input string for the first occurrence of the regular expression specified in the System.Text.RegularExpressions.Regex constructor. |
| Csprintf | 1 | Use C-style String Formatting Commands A wrapper for the C function sprintf, that returns a character vector containing a formatted combination of text and variable values. |
| strsplit | 1 | Split the Elements of a Character Vector Split the elements of a character vector x into substrings according to the matches to substring split within them. |
| str_trim | 1 | Remove whitespace str_trim() removes whitespace from start and end of string; str_squish() removes whitespace at the start and end, and replaces all internal whitespace with a… |
| str_squish | 1 | Remove whitespace str_trim() removes whitespace from start and end of string; str_squish() removes whitespace at the start and end, and replaces all internal whitespace with a… |
| str_remove | 1 | Remove matched patterns Remove matches, i.e. |
| paste0 | 1 | Concatenate Strings Concatenate vectors after converting to character. |
| paste | 1 | Concatenate Strings string join with given delimiter, concatenate vectors after converting to character. |
| str_replace | 2 | Pattern Matching and Replacement |
| str_pad | 1 | Pad A String. |
| str_empty | 1 | test of the give character vector is empty string or not |
| tagvalue | 1 | parse the string text in format like tag{delimiter}value |
| tolower | 1 | Character Translation and Casefolding Translate characters in character vectors, in particular from upper to lower case or vice versa. |
| concatenate | 1 | |
| chr | 1 | chr returns the characters corresponding to the specified ASCII codes. |
| substr | 1 | Substrings of a Character Vector Extract or replace substrings in a character vector. |
| randomAsciiStr | 1 | generate random string which is all consist with ascii chars. |
| text_equals | 1 | check of the text equals between two character vector. |
| text_grep | 1 | apply for batch text grep/trimming/strip |
| grepl | 1 | Pattern Matching and Replacement grep, grepv, grepl, regexpr, gregexpr, regexec and gregexec search for matches to argument pattern within each element of a character vector: t… |
| loadJson | 1 | |
| fromBstring | 1 | |
| bencode | 1 | |
| grep | 1 | |
| toupper | 1 | |
| charAt | 1 | |
| utf8_decode | 1 |
03 Members
comvert any object to html text document
| Name | Type | Description |
|---|---|---|
x | Object | - |
env | list | - |
Object, String, Boolean, Nullable(Of Int32), Int32, Justification, Nullable(Of Int32), Boolean, Nullable(Of Boolean), String, Int64, String, Int64, Object, String, Boolean, list, Environment)Encode in a Common Format
Format an R object for pretty printing, notably encoding vector or column elements into a common format.
format is a generic function. Apart from the methods described here there are methods for dates (see format.Date), date-times (see format.POSIXct) and for other classes such as format.octmode and format.dist.
format.data.frame formats the data frame column by column, applying the appropriate method of format for each column. Methods for columns are often similar to as.character but offer more control. Matrix and data-frame columns will be converted to separate columns in the result, and character columns (normally all) will be given class "AsIs". format.factor converts the factor to a character vector and then calls the default method (and so justify applies). format.AsIs deals with columns of complicated objects that have been extracted from a data frame. Character objects and (atomic) matrices are passed to the default method (and so width does not apply). Otherwise it calls toString to convert the object to character (if a vector or list, element by element) and then right-justifies the result. Justification for character vectors (and objects converted to character vectors by their methods) is done on display width (see nchar), taking double-width characters and the rendering of special characters (as escape sequences, including escaping backslash but not double quote: see print.default) into account. Thus the width is as displayed by print(quote = FALSE) and not as displayed by cat. Character strings are padded with blanks to the display width of the widest. (If na.encode = FALSE missing character strings are not included in the width computations and are not encoded.) Numeric vectors are encoded with the minimum number of decimal places needed to display all the elements to at least the digits significant digits. However, if all the elements then have trailing zeroes, the number of decimal places is reduced until at least one element has a non-zero final digit; see also the argument documentation for big., small. etc, above. See the note in print.default about digits >= 16. Raw vectors are converted to their 2-digit hexadecimal representation by as.character. format.default(x) now provides a “minimal” string when isS4(x) is true. While the internal code respects the option getOption("OutDec") for the ‘decimal mark’ in general, decimal.mark takes precedence over that option. Similarly, scientific takes precedence over getOption("scipen").
| Name | Type | Description |
|---|---|---|
x | Object | any R object (conceptually); typically numeric. |
trim | String | logical; if FALSE, logical, numeric And complex values are right-justified to a common width If True the leading blanks For justification are suppressed. |
digits | Boolean | a positive Integer indicating how many significant digits are To be used For numeric And complex x. The Default, NULL, uses getOption("digits"). This Is a suggestion: enough decimal places will be used so that the smallest (in magnitude) number has this many significant digits, And also to satisfy nsmall. (For more, notably the interpretation for complex numbers see signif.) |
nsmall | Nullable(Of Int32) | the minimum number of digits to the right of the decimal point in formatting real/complex numbers in non-scientific formats. Allowed values are 0 <= nsmall <= 20. |
justify | Int32 | should a character vector be left-justified (the Default), right-justified, centred Or left alone. Can be abbreviated. |
width | Justification | Default method: the minimum field width Or NULL Or 0 For no restriction. AsIs method: the maximum field width For non-character objects. NULL corresponds To the Default 12. |
na_encode | Nullable(Of Int32) | logical: should NA strings be encoded? Note this only applies To elements Of character vectors, Not To numerical, complex nor logical NAs, which are always encoded As "NA". |
scientific | Boolean | either a logical specifying whether elements of a real or complex vector should be encoded in scientific format, or an integer penalty (see options("scipen")). Missing values correspond to the current default penalty. |
big_mark | Nullable(Of Boolean) | used for prettying (longish) numerical and complex sequences. Passed to prettyNum: that help page explains the details. |
big_interval | String | used for prettying (longish) numerical and complex sequences. Passed to prettyNum: that help page explains the details. |
small_mark | Int64 | used for prettying (longish) numerical and complex sequences. Passed to prettyNum: that help page explains the details. |
small_interval | String | used for prettying (longish) numerical and complex sequences. Passed to prettyNum: that help page explains the details. |
decimal_mark | Int64 | used for prettying (longish) numerical and complex sequences. Passed to prettyNum: that help page explains the details. |
zero_print | Object | used for prettying (longish) numerical and complex sequences. Passed to prettyNum: that help page explains the details. |
drop0trailing | String | used for prettying (longish) numerical and complex sequences. Passed to prettyNum: that help page explains the details. |
args | Boolean | further arguments passed To Or from other methods. |
env | list | - |
An object of similar structure to x containing character representations of the elements of the first argument x in a common format, and in the current locale's encoding.
For character, numeric, complex or factor x, dims and dimnames are preserved on matrices/arrays and names on vectors: no other attributes are copied.
If x is a list, the result is a character vector obtained by applying format.default(x, ...) to each element of the list (after unlisting elements which are themselves lists), and then collapsing the result for each element with paste(collapse = ", "). The defaults in this case are trim = TRUE, justify = "none" since one does not usually want alignment in the collapsed strings.
Convert an R Object to a Character String
This is a helper function for format to produce a single character string describing an R object.
this function just invoke the Object.ToString() method.
| Name | Type | Description |
|---|---|---|
x | Object | The object to be converted. |
env | String | - |
| Name | Type | Description |
|---|---|---|
type | Type | the element type |
load a .NET object from the xml data file
this function will try to parse the object model information from the meta data in the xml data file
| Name | Type | Description |
|---|---|---|
file | Object | any kind of data value inputs:
|
env | Object | - |
Object, Environment)Convert most of the R# object or VB.NET object to xml document string.
| Name | Type | Description |
|---|---|---|
x | Object | - |
env | Environment | - |
Convert most of the R# object or VB.NET object to json string.
| Name | Type | Description |
|---|---|---|
x | Object | - |
compress | Boolean | - |
env | list | - |
encode byte stream or text content into base64 string
| Name | Type | Description |
|---|---|---|
raw | Object | R# object or any supported .NET object:
|
env | Int32 | - |
decode base64 string as text or the raw bytes buffer object.
if the base64 encoded data is text data, that parameter asText_encoding assign of value utf8 usually.
| Name | Type | Description |
|---|---|---|
base64 | String | a string in base64 encode pattern |
asText_encoding | Object | if this parameter is not nothing, then the output will be convert as text |
wrap | Boolean | wrap the result as memory stream object instead of the raw bytes vector output |
env | Environment | - |
this function returns a text data or memory stream buffer object.
String())Decodes URL-encoded string
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
| Name | Type | Description |
|---|---|---|
str | String() | The string to be decoded. |
Returns the decoded string.
Object)Count the Number of Characters (or Bytes or Width)
nchar takes a character vector as an argument and returns a vector whose elements contain the sizes of the corresponding elements of x.
| Name | Type | Description |
|---|---|---|
strs | Object | character vector, or a vector to be coerced to a character vector. Giving a factor is an error. |
Initializes a new instance of the RegularExpression class for the specified regular expression pattern.
| Name | Type | Description |
|---|---|---|
pattern | String | the specified regular expression |
Searches the specified input string for the first occurrence of the regular expression specified in the System.Text.RegularExpressions.Regex constructor.
| Name | Type | Description |
|---|---|---|
regexp | Regex | Represents an immutable regular expression. To browse the .NET Framework source code for this type, see the Reference Source. |
strings | Object | The string to search for a match. |
env | Environment | - |
Use C-style String Formatting Commands
A wrapper for the C function sprintf, that returns a character vector containing a formatted combination of text and variable values.
| Name | Type | Description |
|---|---|---|
format | Object | - |
arguments | Object | - |
env | Environment | - |
Split the Elements of a Character Vector
Split the elements of a character vector x into substrings according to the matches to substring split within them.
| Name | Type | Description |
|---|---|---|
text | String() | character vector, each element of which is to be split. Other inputs, including a factor, will give an error. |
delimiter | Object | character vector (or object which can be coerced to such) containing regular expression(s) (unless fixed = TRUE) to use for splitting. If empty matches occur, in particular if split has length 0, x is split into single characters. If split has length greater than 1, it is re-cycled along x. |
fixed | Boolean | logical. If TRUE match split exactly, otherwise use regular expressions. Has priority over perl. |
env | Boolean | - |
Object, Object)Remove whitespace
str_trim() removes whitespace from start and end of string; str_squish() removes whitespace at the start and end, and replaces all internal whitespace with a single space.
| Name | Type | Description |
|---|---|---|
string | Object | Input vector. Either a character vector, Or something coercible To one. |
side | Object | Side on which to remove whitespace: "left", "right", or "both", the default. |
A character vector the same length as string.
Object)Remove whitespace
str_trim() removes whitespace from start and end of string; str_squish() removes whitespace at the start and end, and replaces all internal whitespace with a single space.
| Name | Type | Description |
|---|---|---|
string | Object | Input vector. Either a character vector, Or something coercible To one. |
A character vector the same length as string.
Object, String)Remove matched patterns
Remove matches, i.e. replace them with "".
| Name | Type | Description |
|---|---|---|
string | Object | Input vector. Either a character vector, Or something coercible To one. |
pattern | String | Pattern to look for. The Default interpretation Is a regular expression, As described In vignette("regular-expressions"). Use regex() For finer control Of the matching behaviour. Match a fixed String (i.e. by comparing only bytes), Using fixed(). This Is fast, but approximate. Generally, For matching human text, you'll want coll() which respects character matching rules for the specified locale. You can Not match boundaries, including "", With this Function. |
A character vector the same length as string/pattern.
fruits <- c("one apple", "two pears", "three bananas") str_remove(fruits, "[aeiou]")
Concatenate Strings
Concatenate vectors after converting to character. Concatenation happens in two basically different ways, determined by collapse being a string or not.
paste0(..., collapse) is equivalent to paste(..., sep = "", collapse), slightly more efficiently.
| Name | Type | Description |
|---|---|---|
x | list | one or more R objects, to be converted to character vectors. |
collapse | String | an optional character string to separate the results. Not NA_character_. When collapse is a string, the result is always a string (character of length 1). |
env | Environment | - |
A character vector of the concatenated values. This will be of length zero if all the objects are, unless collapse is non-NULL, in which case it is "" (a single empty string).
If any input into an element Of the result Is In UTF-8 (And none are declared With encoding "bytes", see Encoding), that element will be In UTF-8, otherwise In the current encoding In which Case the encoding Of the element Is declared If the current locale Is either Latin-1 Or UTF-8, at least one Of the corresponding inputs (including separators) had a declared encoding And all inputs were either ASCII Or declared.
If an input into an element Is declared With encoding "bytes", no translation will be done Of any Of the elements And the resulting element will have encoding "bytes". If collapse Is non-NULL, this applies also To the second, collapsing, phase, but some translation may have been done In pasting Object together In the first phase.
Concatenate Strings
string join with given delimiter, concatenate vectors after converting to character.
paste converts its arguments (via as.character) to character strings, and concatenates them (separating them by the string given by sep). If the arguments are vectors, they are concatenated term-by-term to give a character vector result. Vector arguments are recycled as needed, with zero-length arguments being recycled to "" only if recycle0 is not true or collapse is not NULL.
Note that paste() coerces NA_character_, the character missing value, To "NA" which may seem undesirable, e.g., When pasting two character vectors, Or very desirable, e.g. In paste("the value of p is ", p).
paste0(..., collapse) Is equivalent to paste(..., sep = "", collapse), slightly more efficiently.
If a value Is specified For collapse, the values In the result are Then concatenated into a Single String, With the elements being separated by the value Of collapse.
| Name | Type | Description |
|---|---|---|
x | list | one Or more R objects, to be converted to character vectors. |
sep | String | a character String To separate the terms. Not NA_character_. |
collapse | String | an optional character string to separate the results. Not NA_character_. |
A character vector of the concatenated values. This will be of length zero if all the objects are, unless collapse is non-NULL, in which case it is "" (a single empty string).
If any input into an element Of the result Is In UTF-8 (And none are declared With encoding "bytes", see Encoding), that element will be In UTF-8, otherwise In the current encoding In which Case the encoding Of the element Is declared If the current locale Is either Latin-1 Or UTF-8, at least one Of the corresponding inputs (including separators) had a declared encoding And all inputs were either ASCII Or declared.
If an input into an element Is declared With encoding "bytes", no translation will be done Of any Of the elements And the resulting element will have encoding "bytes". If collapse Is non-NULL, this applies also To the second, collapsing, phase, but some translation may have been done In pasting Object together In the first phase.
Pattern Matching and Replacement
| Name | Type | Description |
|---|---|---|
subj | String() | a character vector |
search$ | String | - |
replaceAs | String | - |
regexp | Boolean | the search target is a regex pattern expression? |
env | Environment | - |
String(), String, String, Boolean)The str_replace() function from the stringr package in R can be used to replace matched patterns in a string.
| Name | Type | Description |
|---|---|---|
strings | String() | Character vector |
pattern | String | Pattern to look for |
replacement | String | A character vector of replacements |
String(), Int32, str_padSides, Char)Pad A String.
| Name | Type | Description |
|---|---|---|
string | String() | A character vector. |
width | Int32 | Minimum width of padded strings. |
side | str_padSides | Side on which padding character is added (left, right or both). |
pad | Char | Single padding character (default is a space). |
String(), Boolean, Boolean)test of the give character vector is empty string or not
| Name | Type | Description |
|---|---|---|
x | String() | a character vector that contains multiple string for do the test |
a logical vector of the test result. TRUE means the given string is asserted as an empty factor.
parse the string text in format like tag{delimiter}value
| Name | Type | Description |
|---|---|---|
string | String() | - |
delimiter | String | - |
trim_value | Boolean | - |
env | Boolean | - |
Object, Environment)Character Translation and Casefolding
Translate characters in character vectors, in particular from upper to lower case or vice versa.
chartr translates each character in x that is specified in old to the corresponding character specified in new. Ranges are supported in the specifications, but character classes and repeated characters are not. If old contains more characters than new, an error is signaled; if it contains fewer characters, the extra characters at the end of new are ignored.
tolower And toupper convert upper-case characters in a character vector to lower-case, Or vice versa. Non-alphabetic characters are left unchanged.
| Name | Type | Description |
|---|---|---|
x | Object | a character vector, or an object that can be coerced to character by |
A character vector of the same length and with the same attributes as x (after possible coercion).
Elements of the result will be have the encoding declared as that of the current locale (see Encoding) if the corresponding input had a declared encoding And the current locale Is either Latin-1 Or UTF-8. The result will be in the current locale's encoding unless the corresponding input was in UTF-8, when it will be in UTF-8 when the system has Unicode wide characters.
Object, String, String, String, Boolean, Boolean)| Name | Type | Description |
|---|---|---|
data | Object | - |
comma | String | - |
andalso | String | - |
etc | String | - |
joinSpace | Boolean | - |
enUS | Boolean | - |
if the input data list contains no elements or it is nothing, then this function will returns nothing
Int32())chr returns the characters corresponding to the specified ASCII codes.
| Name | Type | Description |
|---|---|---|
ascii | Int32() | vector or list of vectors containing integer ASCII codes |
Substrings of a Character Vector
Extract or replace substrings in a character vector.
| Name | Type | Description |
|---|---|---|
x | Object | a character vector. |
start | Int32 | integer. The first element To be replaced. |
stop | Int32 | integer. The last element to be replaced. |
For substr, a character vector of the same length and with the same attributes as x (after possible coercion).
Int32, Int32, Boolean)generate random string which is all consist with ascii chars.
| Name | Type | Description |
|---|---|---|
nchar | Int32 | - |
count | Int32 | - |
check of the text equals between two character vector.
| Name | Type | Description |
|---|---|---|
x | Object | should be a character vector |
y | Object | another character vector |
null_equals | Boolean | null string value as equals? example as NULL is equals to NULL, or NULL is equals to empty string |
empty_equals | Boolean | empty factor string value as equals? example as NA is equals to n/a, or NA is equals to NULL, etc. |
env | Environment | - |
apply for batch text grep/trimming/strip
the text grep regexp pattern token should be wrapped inside a bracket: $(regexp_token), any text outside this bracket will be treated as an regular string.
example as the token parser string: $(\d+)_$(\[\d*M.*\]\d*[+-]) will be split into 3 tokens: $(\d+) for matches integer string token, _ will always produce a regular string token _, $(\[\d*M.*\]\d*[+-]) for matches another string token that wrapped with bracket [] and ends with signed integer number as suffix.
| Name | Type | Description |
|---|---|---|
grep_regexp | String | a regular expression value for produce new text value. |
x | Object | should be a character vector for apply such operation. omit this parameter value will create a lambda function for grep text. |
env | Environment | - |
let names = ["258_Herniarin_[M+H2O+H]+" "993_Geranyl acetate_[M+H2O+H]+" "3229_Glycerol_[M+NH4]+" "398587_Cytidine_[M+H]+" "398007_Adenosine[M+H]+"]; let grep = "$(\d+)$([\dM.]\d*[+-])";
print(text_grep(grep, names));
[1] "258[M+H2O+H]+" "993[M+H2O+H]+" "3229[M+NH4]+" "398587[M+H]+" "398007_[M+H]+"
String, Object, Boolean, Boolean, Boolean, Boolean)Pattern Matching and Replacement
grep, grepv, grepl, regexpr, gregexpr, regexec and gregexec search for matches to argument pattern within each element of a character vector: they differ in the format of and amount of detail in the results.
sub and gsub perform replacement of the first and all matches respectively within each element of a character vector.
Arguments which should be character strings or character vectors are coerced to character if possible. Each of these functions operates in one of three modes:
- fixed = TRUE: use exact matching.
- perl = TRUE: use Perl-style regular expressions.
- fixed = FALSE, perl = FALSE: use POSIX 1003.2 extended regular expressions (the default).
See the help pages on regular expression for details of the different types of regular expressions. The two *sub functions differ only in that sub replaces only the first occurrence of a pattern whereas gsub replaces all occurrences. If replacement contains backreferences which are not defined in pattern the result is undefined (but most often the backreference is taken to be "").
For regexpr, gregexpr, regexec and gregexec it is an error for pattern to be NA, otherwise NA is permitted and gives an NA match. grep and grepv only differ in the default of the value argument.Both grep and grepl take missing values in x as not matching a non-missing pattern.
The main effect of useBytes = TRUE is to avoid errors/warnings about invalid inputs and spurious matches in multibyte locales, but for regexpr it changes the interpretation of the output. It inhibits the conversion of inputs with marked encodings, and is forced if any input is found which is marked as "bytes" (see Encoding).
Caseless matching does not make much sense for bytes in a multibyte locale, and you should expect it only to work for ASCII characters if useBytes = TRUE. regexpr and gregexpr with perl = TRUE allow Python-style named captures, but not for long vector inputs.
Invalid inputs in the current locale are warned about up to 5 times.
Caseless matching with perl = TRUE for non-ASCII characters depends on the PCRE library being compiled with ‘Unicode property support’, which PCRE2 is by default.
| Name | Type | Description |
|---|---|---|
pattern | String | character string containing a regular expression (or character string for fixed = TRUE) to be matched in the given character vector. Coerced by as.character to a character string if possible. If a character vector of length 2 or more is supplied, the first element is used with a warning. Missing values are allowed except for regexpr, gregexpr and regexec. |
x | Object | a character vector where matches are sought, or an object which can be coerced by as.character to a character vector. Long vectors are supported. |
ignore_case | Boolean | logical. if FALSE, the pattern matching Is case sensitive And if TRUE, case Is ignored during matching. |
perl | Boolean | logical. Should Perl-compatible regexps be used? |
fixed | Boolean | logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments. |
useBytes | Boolean | logical. If TRUE the matching Is done byte-by-byte rather than character-by-character. See 'Details’. |
grepl returns a logical vector (match or not for each element of x).
String)Object, Environment)Object, Environment)String, Int32)Object, Environment)