Synergy/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
String.IsNullorWhitespace(@string) , boolean
String.Trim() ,string (exists prior to version 11)
String.TrimStart(@string) ,string String.TrimEnd(@string) ,string
String.Insert(N, @string) ,string
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
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)
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)
String.Split(A1) ,[#]string String.Split(A1, StringSplitOptions) ,[#]string (see below for the StringSplitOptions enumeration)
String.Replace(A, A) ,string
String.Replace(@string, @string) ,string (exists prior to version 11)
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 = “Bob” stringarray = “Mary” stringarray = “Joe” stringarray = “Jack” stringarray = “Fred” stringarray = “Henry” stringarray = “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.