With any of the following variants all white-space characters are defined as splitting character :
string[] words = phrase.Split(null);
string[] words = phrase.Split(new char[0]);
string[] words = phrase.Split((char[])null);
string[] words = phrase.Split(default(Char[]));
string[] words = phrase.Split(null as char[]);
But is there also a way to define whitespace AND additional separator characters like comma ( , ) or hyphen (-) without nested calls of String.Split and without explicitly defining each of the white space characters ?
The goal is high performance, rather than concise code
You could use Regex.Split
instead, which allows you to split on a Regex pattern:
string[] words = Regex.Split(phrase, "[\s,-]");
The pattern [\\s,-]
will split on any whitespace ( \\s
), or a comma or hyphen literal ( ,
, -
)
Be sure to add a reference to System.Text.RegularExpressions
using System.Text.RegularExpressions;
As the official documentation says:
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, and optionally the Compare method, to locate a substring within a string.
If you need high performance you should use Span<string>
and the methods IndexOf
and Slice
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.