-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy path170_string.Rmd
executable file
·98 lines (59 loc) · 2.83 KB
/
170_string.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# String
`String` is a scalar type corresponding to the element of `CharacterVector`. `String` can also handle NA values (`NA_STRING`) which are not supported by the C character string `char*` and the C++ string `std::string`.
## Creating String object
There are roughly three ways to create a `String` object, as follows. The first method is to create from a C/C++ string, the second is to create it from another `String` object, and the third is to create it from one element of a `CharacterVector`.
```cpp
// Creating from C string
String s("X"); // "X"
// Creating from Rcpp String
String s(str);
//Creating from single element of CharacterVector object
String s(char_vec[0])
```
## Operators {#operators-string}
The `+=` operator is defined in `String` type. This allows you to combine another string object at the end of the string. (Note that the `+` operator is not defined)
```
// Creating String object
String s("A");
// Conbining a string
s += "B";
Rcout << s << "\n"; // "AB"
```
## Member functions {#member-functions-string}
Note: The member functions `replace_first()`, `replace_last()`, `replace_all()` do not just return the replaced character string, but instead rewrite the value of this object.
### replace_first( str, new_str )
Replace first substring that matches the string `str` with the string `new_str`.
### replace_last( str, new_str )
Replace last substring that matches the string `str` with the string `new_str`.
### replace_all( str, new_str )
Replace all substrings that matches the string `str` with the string `new_str`.
### push_back(str)
Combine the string `str` to the end of this `String` object. (Same as += operator)
### push_front(str)
Combine the string str at the beginning of this `String` object.
### set_na()
Set NA value to this `String` object.
### get_cstring()
Convert the string of this String object into a C character string constant (const char*) and return it.
### get_encoding()
Returns the character encoding. The encoding is represented by [`cetype_t`](https://github.com/wch/r-source/blob/bf0a0a9d12f2ce5d66673dc32cd253524f3270bf/src/include/Rinternals.h#L928-L935).
### set_encoding(enc)
Set the character encoding specified by [`cetype_t`](https://github.com/wch/r-source/blob/bf0a0a9d12f2ce5d66673dc32cd253524f3270bf/src/include/Rinternals.h#L928-L935).
### Code example {#code-example-string}
```
// [[Rcpp::export]]
void rcpp_replace(){
// Replace only at the first occurrence of "ab"
String s("abcdabcd");
s.replace_first("ab", "AB");
Rcout << s.get_cstring() << "\n"; // ABcdabcd
// Replace only at the last occurrence of "ab"
s="abcdabcd";
s.replace_last("ab", "AB");
Rcout << s.get_cstring() << "\n"; // abcdABcd
// Replace every occurrence of "ab"
s="abcdabcd";
s.replace_all("ab", "AB");
Rcout << s.get_cstring() << "\n"; // ABcdABcd
}
```