System.Text.StringBuilder sb =
new
System.Text.StringBuilder(text.Length);
string
filterText =
"需要过滤的脏字 以|分开"
;
string
[] filterData = filterText.Split(
'|'
);
foreach
(var item
in
filterData)
{
char
value = item[0];
if
(dicList.ContainsKey(value))
dicList[value].Add(item);
else
dicList.Add(value,
new
List<
string
>() { item });
}
int
count = text.Length;
for
(
int
i = 0; i < count; i++)
{
char
word = text[i];
if
(dicList.ContainsKey(word))
{
int
num = 0;
var data = dicList[word].OrderBy(g => g.Length);
foreach
(var wordbook
in
data)
{
if
(i + wordbook.Length <= count)
{
string
result = text.Substring(i, wordbook.Length);
if
(result == wordbook)
{
num = 1;
sb.Append(GetString(result));
i = i + wordbook.Length - 1;
break
;
}
}
}
if
(num == 0)
sb.Append(word);
}
else
sb.Append(word);
}
return
sb.ToString();
}
/// <summary>
/// 替换星号
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private
static
string
GetString(
string
value)
{
string
starNum =
string
.Empty;
for
(
int
i = 0; i < value.Length; i++)
{
starNum +=
"*"
;
}
return
starNum;
}