【发布时间】:2014-10-13 07:59:45
【问题描述】:
我有一个方法被调用了数千次,但目前运行速度很慢。有更好的技术吗?允许使用不安全的方法。
它只是检查输入字符串的字母是否可以由其他字符串的字母组成,就像一个字谜检查器。
这就是我的方法目前的样子。
public static bool IsStringMadeOf(this string str, string from)
{
for (int i = 0; i < str.Length; i++)
{
int index = from.IndexOf(str[i]);
if (index != -1)
{
from = from.Remove(index, 1);
}
else
{
return false;
}
}
return true;
}
对于我的测试用例,结果应该如下:
str | from | result
-------------------------------
SLOW | WSOL | true
SLOW | WTOL | false
ASIA | XZYABSTRIB | false
ASIA | XZYABSTRIAB | true
【问题讨论】:
标签: string c#-4.0 extension-methods