-
See Also
- CreateDataStructure
- DataStructure
-
- Compiled Types
- String
-
- Data Structures
- BitVector
- Related Guides
-
-
See Also
- CreateDataStructure
- DataStructure
-
- Compiled Types
- String
-
- Data Structures
- BitVector
- Related Guides
-
See Also
"StringVector" (Data Structure)
"StringVector"
represents a vector of strings.
Details
- A "StringVector" is an efficient immutable representation of a list or vector of strings.
-
CreateDataStructure["StringVector",{s1,…}] create a new "StringVector" from a list of strings Typed[x,"StringVector"] give x the type "StringVector" - For a data structure of type "StringVector", the following operations can be used:
-
ds["Part",i] give the i
part of dstime: O(1) ds["Pick",bvec] pick the elements of ds corresponding to the set bits of the "BitVector" bvec time: O(n) ds["Join",ds1] join the vectors ds and ds1 time: O(n) ds["StringByteCount"] give StringByteCount for each string element in ds time: O(n) ds["StringLength"] give StringLength for each string element in ds time: O(n) ds["StringCount",patt] give StringCount for each element of ds for the general string expression patt time: O(n) ds["StringMatchQ",patt] return a "BitVector" with bit i set for each element si of ds for which StringMatchQ[si,patt] is True time: O(n) ds["StringFreeQ",patt] return a "BitVector" with bit i set for each element si of ds for which StringFreeQ[si,patt] is True time: O(n) ds["StringContainsQ",patt] return a "BitVector" with bit i set for each element si of ds for which StringContainsQ[si,patt] is True time: O(n) ds["StringStartsQ",patt] return a "BitVector" with bit i set for each element si of ds for which StringStartsQ[si,patt] is True time: O(n) ds["StringEndsQ",patt] return a "BitVector" with bit i set for each element si of ds for which StringEndsQ[si,patt] is True time: O(n) ds["Visualization"] return a visualization of ds time: O(n) - The following functions are also supported:
-
dsi===dsj True, if dsi equals dsj FullForm[ds] full form of ds Information[ds] information about ds InputForm[ds] input form of ds Normal[ds] convert ds to a normal expression
Examples
open all close allBasic Examples (4)
A new "StringVector" can be created with CreateDataStructure:
ds = CreateDataStructure["StringVector", {"For", "each", "ϵ > 0", "there", "exists", "δ", "such", "that", "…"}]Get the third element as a string:
ds["Part", 3]Get the last three elements in reverse order as a "StringVector":
ds["Part", -1 ;; -3 ;; -1]Change the result to a list of strings:
Normal[%]Get the string length for each element:
ds["StringLength"]Get the string byte count for each element:
ds["StringByteCount"]Count the number of English vowels in each element:
ds["StringCount", RegularExpression["[aeiou]"]]Determine which elements have the letter "a":
ds["StringContainsQ", "a"]ds1 = ds["Pick", %]ds1//NormalSelect the elements do not have the letter "e":
ds2 = ds["Pick", ds["StringFreeQ", "e"]]Normal[%]"StringVector" will typically use less memory than a list of strings:
words = BlockRandom[RandomWord[10000], RandomSeeding -> 314];
ds = CreateDataStructure["StringVector", words]{ByteCount[words], ByteCount[ds]}Lookup of a literal string can be much faster with "StringVector" than for a list of strings:
BlockRandom[
words = RandomWord[100];
wordlist = RandomChoice[words, 12345],
RandomSeeding -> 314
];
ds = CreateDataStructure["StringVector", wordlist];
literal = ds[[-1]]RepeatedTiming[btest = ds["StringMatchQ", literal]]RepeatedTiming[test = StringMatchQ[wordlist, literal];]Selecting elements can be much faster with "StringVector" than for a list of strings:
BlockRandom[
words = RandomWord[100];
wordlist = RandomChoice[words, 12345],
RandomSeeding -> 314
];
ds = CreateDataStructure["StringVector", wordlist];Select the "StringVector" elements that start with "r" and end with "e":
RepeatedTiming[dselected = ds["Pick", ds["StringContainsQ", "r*e"]]]Use Select with the list of strings:
RepeatedTiming[selected = Select[wordlist, StringContainsQ[#, "r*e"]&];]Normal[dselected] === selectedScope (1)
Information (1)
A new "StringVector" can be created with CreateDataStructure:
ds = CreateDataStructure["StringVector", {"This", "is", "a", "string", "vector"}]Information about the data structure ds:
Information[ds]Related Guides
History
Introduced in 2024 (14.0)