Videos
The following apex code can be used to extract 'Equal' enclosed between and tags :
String inputText = '<Meaning>Equal</Meaning>';
String patternString = '<Meaning>(.*?)</Meaning>';
String result = '';
System.Pattern patternTxt = System.Pattern.compile(patternString);
Matcher matcherTxt = patternTxt.matcher(inputText);
if (matcherTxt.find()) {
result = matcherTxt.group(1);
}
System.debug(result); // This will output 'Equal'
I think that you want to ask is "how can I extract a string that I don't know from the middle of a string where I know its borders". Is this right? If so, then you can use removeStart and removeEnd to remove the <Meaning> and </Meaning>, respectively. This is assuming you know the contents of the borders in your string, but don't know what the content you want to get (the "Equal" string).
String s = '<b>text</b>';
String result = s.removeStart('<b>').removeEnd('</b>');
If you want to do this in a case-insensitive way, you can optionally replace those two with their counterparts removeStartIgnoreCase and removeEndIgnoreCase.
Object.toString() is unreliable in Apex. Objects that do not explicitly call out toString() as a feature use a particular toString() that was designed to be output in a debug log. Even worse, this default behavior can come from string concatenation on classes that do have a documented toString().
System.debug(''+Blob.valueOf('Hello World')); // Blob[11]
System.debug(Blob.valueOf('Hello World').toString()); // Hello World
You'll find these kinds of problems all over the place. For example, Date and DateTime have the same toString():
Date today = Date.today();
DateTime now = DateTime.now();
String todayString = today.toString();
String nowString = now.toString();
System.debug(todayString); // 2024-01-01 00:00:00
System.debug(nowString); // 2024-01-01 01:02:03
Map, List, and Set all suffer from this same condition. If the default toString() is called, they will truncate themselves after a few values.
In contrast, String.join() is 100% reliable. It will always write out all the values, no matter how long the string ends up being (up to heap size limits). You should always use String.join() when possible to ensure consistent behavior in regards to how the string will turn out.
Yes, what you've encountered is a known behavior in Apex when using the toString() method on collections, such as Set<String>. When the string representation of a collection reaches a certain length, Salesforce abbreviates the output by inserting ellipses (...) towards the end. This is to prevent excessive memory usage or potential performance issues with very large collections.
Your initial approach directly converted the Set to a string and manipulated it to remove the curly braces, which are part of the default output format for sets in toString(). As your set grew, the abbreviation issue manifested, indicating Salesforce's internal limit was reached, causing it to truncate the string representation.
Switching to String.join(new List<String>(fields), ', ') resolved the issue because this method explicitly joins the elements of the collection into a single string without adding extra formatting or risking abbreviation. String.join() is designed for precisely this scenario, where a predictable and complete string representation is needed, without the addition of formatting characters like braces or the risk of encountering the ellipsis abbreviation.
This approach is particularly useful for dynamically constructing SOQL queries or similar tasks where an accurate representation of collection elements as a string is required.
It's worth noting that Salesforce documentation doesn't explicitly state the character limit for the toString() abbreviation, likely because it could be subject to Salesforce's internal implementation details or limits, which may change. Therefore, for tasks requiring the full representation of collection elements as a string, methods like String.join() are recommended.