-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathStringUtils.cs
More file actions
31 lines (27 loc) · 852 Bytes
/
StringUtils.cs
File metadata and controls
31 lines (27 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Diagnostics.Contracts;
namespace DataStructures.Utils
{
public static class StringUtils
{
public static string CommonPrefix(this string str1, string str2)
{
Contract.Ensures(Contract.Result<string>() != null);
return str1.Substring(0, str1.CommonPrefixLength(str2));
}
public static int CommonPrefixLength(this string str1, string str2)
{
Contract.Requires<ArgumentNullException>(str2 != null);
Contract.Ensures(Contract.Result<int>() >= 0);
int count = 0;
for (int i = 0; i < str1.Length && i < str2.Length; i++, count++)
{
if (str1[i] != str2[i])
{
break;
}
}
return count;
}
}
}