There is no such method built into the .NET standard. You can easily recreate it in C#.
From the docs, this is what partition
is supposed to do:
Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.
We can use IndexOf
and Substring
to implement this in C#.
public static class StringExtensions {
public static (string, string, string) Partition(this string str, string sep) {
int index = str.IndexOf(sep); // find the index
if (index >= 0) { // if found, get the substrings
return (str.Substring(0, index), sep, str.Substring(index + sep.Length));
} else {
return (str, "", "");
}
}
}
Usage:
var result = "Hello, World".Partition(", ");
Console.WriteLine(result.Item1); // Hello
Console.WriteLine(result.Item3); // World