Count occurrences of a substring: Difference between revisions

Content deleted Content added
m →‎{{header|C sharp}}: Bug fix for null testString
Line 488: Line 488:
}</lang>
}</lang>


Using C# 6.0's expression-bodied member and null-conditional operator features:
Using C# 6.0's expression-bodied member, null-conditional operator, and coalesce operator features:


<lang c sharp>using System;
<lang c sharp>using System;
Line 494: Line 494:
{
{
public static int CountSubStrings(this string testString, string testSubstring) =>
public static int CountSubStrings(this string testString, string testSubstring) =>
testString?.Split(new string[] { testSubstring }, StringSplitOptions.None)?.Length - 1;
testString?.Split(new string[] { testSubstring }, StringSplitOptions.None)?.Length - 1 ?? 0;
}</lang>
}</lang>