Harmony Core Office Hours, August 2019
August 7, 2019Enhanced CodeGen CreateFile utility
August 26, 2019Synergy/DE 11 is bringing many new features, and among those additions are some exciting new abilities within the String class. All of these powerful new features are available in both traditional and Synergy .NET.
When dealing with strings, keep in mind that a string is immutable (fancy word for unchangeable), so any time a string variable is changed, a new string variable is created.
First, let me list the new methods added, along with existing methods that include new overloads to the String class:
String.IsNullorEmpty(@string) , boolean
- Parameter: String to interrogate for null or empty value
- Return value: Boolean value indicating whether the string is null or empty
String.IsNullorWhitespace(@string) , boolean
- Parameter: String to interrogate for null or white-space characters
- Return value: Boolean value indicating whether the string contains null or white-space characters
New overloads to give precise control on how a string is trimmed:
String.Trim() ,string (exists prior to version 11)
- Parameter: None
- Return value: String with leading and trailing white-space characters removed
String.Trim(@string) ,string
- Parameter: Character(s) to be removed from the string’s beginning and end
- Return value: Returned string with passed-in character(s) removed from the beginning and end
String.TrimStart(@string) ,string
String.TrimEnd(@string) ,string
- Parameter: Character(s) to remove from either the string’s start or end
- Return value: String with all instances of character(s) removed from start or end
New methods to control how characters are removed or inserted into a string:
String.Remove(N) ,string
- Parameter: Starting position in the string to remove all characters until its end
- Return value: String with all characters from passed starting position to end removed
String.Remove(N) ,string
- Parameter: Starting position in the string to remove all characters until its end
- Return value: String with all characters from passed starting position to end removed
String.Insert(N, @string) ,string
- Parameters:
- Starting position to insert string passed as parameter 2
- String to be inserted
- Return value: Returned string with sting passed in parameter 2 inserted
New overloads to give more control on concatenating strings together:
String.Concat(@string, @string) ,string (exists prior to version 11)
String.Concat(@string, @string, @string) ,string
String.Concat(@string, @string, @string, @string) ,string
String.Concat([#]@string) ,string
- Parameters:
- Two strings to concatenate together
- Three strings to concatenate together
- Four strings to concatenate together
- Dynamic string array to concatenate all elements together
- Return value: Returned string after concatenation operation
New overloads to locate the position of a substring in a string:
String.IndexOf(A1) ,int (exists prior to version 11)
String.IndexOf(A1, N) ,int
String.IndexOf(A1, N, N) ,int (exists prior to version 11)
- Parameters:
- Character(s) to find in string
- Character(s) to find in string starting at position specified in parameter 2
- Character(s) to find in string starting at position specified in parameter 2 for the length of parameter 3
- Return value: Integer value with first position of passed character(s)
New overloads to find the last occurrence of a substring in a string:
String.LastIndexOf(A1) ,int (exists prior to version 11)
String.LastIndexOf(A1,N) ,int
String.LastIndexOf(A1,N,N) ,int (exists prior to version 11)
Parameters:
- Parameters
- Numeric value of the starting position to begin returning the substring
- Numeric value of the length of the substring to return
- Return value: Returned substring with value from the specified starting and length values
New methods to create an array from a delimited string:
String.Split(A1) ,[#]string
String.Split(A1, StringSplitOptions) ,[#]string (see below for the StringSplitOptions enumeration)
- Parameters:
- Character(s) to use a delimiter
- Enumeration StringSplitOptions to control if empty elements are created
- Return value: Dynamic string array to be populated with the contents of the passed string
New overload to use the replace method on alphas:
String.Replace(A, A) ,string
- Parameters:
- Alpha value to be replaced by parameter 2
- Alpha value to replace all occurrences of parameter 1 in string
- Return value: Returned string with all occurrences of parameter 1 replaced by parameter 2
String.Replace(@string, @string) ,string (exists prior to version 11)
- Parameters
- String value to be replaced by parameter 2
- String value to replace all occurrences of parameter 1 in string
- Return value: Returned string with all occurrences of parameter 1 replaced by parameter 2
And finally, the enumeration StringSplitOptions, which is used with the Split method to control if empty elements are created when creating the string array:
public enum StringSplitOptions
None ,0
RemoveEmptyEntries ,1
You can find details on all String members in the System.String topic in the Synergy/DE documentation.
As I mentioned earlier, some of the above are brand new methods (Insert, IsNullorWhitespace, Remove, Split, TrimEnd, TrimStart) added to the System.String class, while others are new overload methods added to existing methods to provide additional functionality. Interestingly, the runtime itself will make use of the String.Concat method when strings are added together (e.g., string1 + string), as it is efficient and generates fewer temporary variables.
Now let’s take a look at one of the new methods. Using the new Split method, we can populate a dynamic array from a delimited string. Say we have a string containing the following:
mystring = “Bob, Mary, Joe, Jack,,,, Fred, Henry, Tom”
We’d like for the values between the commas to be elements in an array, but we don’t want the commas without values between them to become empty elements in the array. This can easily be accomplished with the String.Split method using the syntax below:
stringarray = mystring.split(“,” , StringSplitOptions.RemoveEmptyEntries)
Passing the quoted comma as the first parameter tells the Split method that a comma is the delimiter to look for in the string. Also, for even more control, the delimiter can consist of any number of characters. There is no need to instantiate the dynamic array, as the dynamic array will automatically be instantiated by the Split method. The result of the above operation will be that the dynamic array stringarray will contain elements with the names that are separated by commas in the string mystring and no empty elements (due to passing StringSplitOptions.RemoveEmptyEntries as the second parameter) for the commas with no values between them:
stringarray[1] = “Bob”
stringarray[2] = “Mary”
stringarray[3] = “Joe”
stringarray[4] = “Jack”
stringarray[5] = “Fred”
stringarray[6] = “Henry”
stringarray[7] = “Tom”
As you can see, the enhancements to the String class are more than enough reason to upgrade to Synergy/DE 11 when it’s released. If you can’t wait and want to try these new capabilities now, the version 11 beta is available in the Resource Center for download. Don’t forget to set your Synergy/DE documentation to the “Beta” version in the version drop-down near the top of the screen.
And wait, there’s more! Now that you’re aware of the new and powerful methods added to the String class, be sure to also check out the new StringBuilder class added to version 11.