Operators for Substring of Text
-
Prerequisite
Introduction
You learned about the operators for characters in text before. You can also operate on “substrings” of text. A “substring” is a segment of the text. It might contain one or more characters from the text that are connected with each other.
For example, for the text “abcde”, you can take the characters in positions 2, 3 and 4 to create a substring “bcd”:
Note that you can not take “b” and “d” to form a substring of “abcde”. Although they are both in the text, they are not connected (since they are separated by “c”).
Let’s look at some of the operator blocks for working with substrings of text below.
Whether a Text Contains a Substring
A most common use case is to check if a text contains a substring in it. For example, for the text “abcde”, “ab” is a substring, “bd” is not. Note that uppercase and lowercase letters are treated as the same, so “BC” is the same as “bc”.
Position of a Substring in a Text
When a substring is in a text, you can also get its starting position within the text. For example, the substring “bc” is in position 2 to 3 in the text “abcde”, so its starting position is 2.
If the substring is not found in the text, then a value of 0 will be returned. Note that uppercase and lowercase letters are treated as the same.
Extract a Substring from a Text
If you know the starting and ending positions of the substring you are looking for, you can use the “substring of” block to extract that substring. If the positions are not valid, then an empty value will be returned.
Replace a Substring in a Text
You can replace a substring of a text with another text. For example, in the text “How do you do?”, the substring “do” appears 2 times. We can replace them with another text, like “did”:
The 3 inputs are the substring to be replaced, the new text to replace with, and the text to be changed.