我有一个大脑放屁..我做错了什么...我的阵列关闭了?C#字符串数组字过滤器,我的数组是在索引之外?
public static string CleanBadwordsFromString(string text) {
string badWords = "bunch,of,words,that,do,not,need,to,be,seen";
string[] badChars = badWords.Split(',');
string[] words = text.Split(' ');
int iLength = 0;
string sAttachtoEnd = null;
string cleanedString = "";
int x = 0;
int i = 0;
//loop through our array of bad words
for (i = 0; i <= badChars.Length; i++)
{
//get the length of the bad word
iLength = badChars[i].Length;
//we are going to keep the first letter of the bad word and replace all the other
//letters with *, so we need to find out how many * to use
for (x = 1; x <= iLength - 1; x++)
{
sAttachtoEnd = sAttachtoEnd + "*";
}
//replace any occurences of the bad word with the first letter of it and the
//rest of the letters replace with *
foreach (string s in words)
{
cleanedString =cleanedString + s.Replace(s, s.Substring(s.Length-1) + sAttachtoEnd); //should be: shit = s***
}
sAttachtoEnd = "";
}
return cleanedString;
}
我终于得到了我原来的工作......索引数组只是我的第一个bug ...很快就要去睡觉了。必须替换://用坏字的第一个字母替换坏字的所有出现,并用 替换掉其余字母* text = text.Replace(badChars [i] .ToString(),badChars [i ] .Remove(1,badChars [i] .Length-1)+ sAttachtoEnd);我更喜欢你的解决方案,因为我从来没有想过标点符号或案例......感谢你的教训 – Bryant