If you need all the elements that are between the delimiters, then Split()
is the way to go.
If you only need, say, the second element, and you don’t want to allocate memory for the first and third elements, you can use IndexOf
to find the delimiters then extract the string using SubString
.
More tips on performance can be found here:
Performance Considerations
The Split methods allocate memory for the returned array object and a String object for each array element. If your application requires optimal performance or if managing memory allocation is critical in your application, consider using the IndexOf or IndexOfAny method. You also have the option of using the Compare method to locate a substring within a string.
To split a string at a separator character, use the IndexOf or IndexOfAny method to locate a separator character in the string. To split a string at a separator string, use the IndexOf or IndexOfAny method to locate the first character of the separator string. Then use the Compare method to determine whether the characters after that first character are equal to the remaining characters of the separator string.
In addition, if the same set of characters is used to split strings in multiple Split method calls, consider creating a single array and referencing it in each method call. This significantly reduces the additional overhead of each method call.
solved Concatenation chars VS split method [closed]