In R, strings are treated as vectors of characters, and there isn’t a direct equivalent to Go’s concept of runes. However, we can still demonstrate similar string manipulation techniques using R’s built-in functions and the stringi package for Unicode operations.
Let’s break down the R code and explain its components:
We start by loading the stringi package, which provides advanced string manipulation functions, especially for Unicode strings.
We define a string s containing Thai characters, just like in the original example.
To get the length in bytes, we use nchar(s, type = "bytes").
To print hex values of each byte, we convert the string to raw bytes using charToRaw() and then format it.
To count characters (equivalent to runes in this context), we use nchar(s, type = "chars").
We iterate over each character in the string using a for loop and substr(). We use stri_char_tobytes() to get the Unicode code point.
The examine_char() function demonstrates how to compare characters directly.
Finally, we use stri_split_boundaries() to split the string into individual characters, which is similar to the DecodeRuneInString approach in the original example.
When you run this R script, you’ll get output similar to the following:
This R code demonstrates similar concepts to the original example, showing how to work with Unicode strings, examine individual characters, and handle multi-byte characters in R.