From ddd1740109e7e78bb7f516542d037c8e120f62a1 Mon Sep 17 00:00:00 2001 From: Arjen Baart Date: Thu, 23 May 2019 22:23:49 +0200 Subject: [PATCH] Completed chapter about String class. --- doc/string.xml | 55 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/doc/string.xml b/doc/string.xml index 6755035..cf53e99 100644 --- a/doc/string.xml +++ b/doc/string.xml @@ -137,8 +137,17 @@ x(3,5) = y; // x = "abc12345678ijkl" Note that assigning a string to a zero-length substring will simply insert the a string into another string. Reversely, assigning an empty string to a -substring will remove the characters from the original string. This property -can be used to truncate a string to its first n characters by: +substring will remove the characters from the original string. + + +String x = "abcdefghijkl"; +String y = "12345678"; + +x(3,0) = y; // x = "abc12345678defghijkl" +x(11,5) = ""; // x = "abc12345678ijkl" + + +This property can be used to truncate a string to its first n characters by: x(n, ~x-n) = ""; @@ -235,8 +244,48 @@ array from an istream. I.e., characters are read from the stream until the first whitespace character. + +
+Regular Expressions + +Regular expressions are handled by objects of class regex. +Constructors allow the creation of regex objects from String objects, +literal strings or other regex objects. + + +String pattern("[a-z]+"); +regex word(pattern); +regex number("[0-9]+"); +regex nr(number); + + +Regular expressions are primarily used to find patterns in text strings. +The == operator performs the pattern matching. +A relational expression returns true if the String matches the pattern, +i.e. the pattern matches some part of the text in the String object. + + +regex nr("[0-9]+"); +String x("abcdef""); +String y("abc123def"); + +x == nr; // false +nr == y; // true + + +A regular expression can be used in a substring expression. +The substring selected from the target string is the first part +that matches the regular expression. + + +regex nr("[0-9]+"); +String y("abc123def456ghi"); + +x = y(nr); // x = 123 + +
-SEE ALSO +See Also Bjarne Stroustrup, Section 6.9 DDJ October 1991, pg 24 -- 2.20.1