我有一個大腦放屁..我做錯了什麼...我的陣列關閉了?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